Skip to content

Commit

Permalink
feat!: Rework command validation
Browse files Browse the repository at this point in the history
There's no special flow for validation now, it's just a flag like
commands, help, etc. This is a BREAKING CHANGE because now the
`--validate` flag needs to come after `--`.
  • Loading branch information
juanibiapina committed Jun 14, 2024
1 parent f1aba0c commit d049ffb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ Usage comments in sub 1.x for the purpose of documenting, you can run `sub`
with the `--validate` flag to check if your scripts are compatible with the new
version.

Example:
```
$ sub --name hat --absolute /path/to/cli/root -- --validate
```

### Help, commands and completions

If you used the `help`, `commands` or `completions` subcommands, they are now
Expand Down
1 change: 1 addition & 0 deletions integration/help.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options:
--usage Print usage
-h, --help Print help
--completions Print completions
--validate Validate subcommand
--commands Print subcommands
--extension <extension> Filter subcommands by extension
Expand Down
2 changes: 1 addition & 1 deletion integration/validate.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PROJECT_DIR="$SUB_TEST_DIR/v1"
@test "sub: validates all subcommands in the project directory" {
fixture "v1"

run $SUB_BIN --name main --absolute "$PROJECT_DIR" --validate
run $SUB_BIN --name main --absolute "$PROJECT_DIR" -- --validate

assert_failure
assert_output "$PROJECT_DIR/libexec/invalid-usage: invalid usage string
Expand Down
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ impl Config {
.arg(Arg::new("usage").long("usage").num_args(0).help("Print usage"))
.arg(Arg::new("help").short('h').long("help").num_args(0).help("Print help"))
.arg(Arg::new("completions").long("completions").num_args(0).help("Print completions"))
.arg(Arg::new("validate").long("validate").num_args(0).help("Validate subcommand"))

.arg(Arg::new("commands").long("commands").num_args(0).help("Print subcommands"))
.arg(Arg::new("extension").long("extension").num_args(1).help("Filter subcommands by extension"))
.group(ArgGroup::new("extension_group").args(["extension"]).requires("commands"))

.group(ArgGroup::new("exclusion").args(["commands", "completions", "usage", "help"]).multiple(false).required(false))
.group(ArgGroup::new("exclusion").args(["commands", "completions", "usage", "help", "validate"]).multiple(false).required(false))

.arg(Arg::new("commands_with_args").trailing_var_arg(true).allow_hyphen_values(true).num_args(..))
}
Expand Down
44 changes: 15 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,6 @@ fn main() {

let config = Config::new(sub_cli_args.name, sub_cli_args.root, sub_cli_args.color, sub_cli_args.infer_long_arguments);

if sub_cli_args.validate {
let top_level_command = match subcommand(&config, Vec::new()) {
Ok(subcommand) => subcommand,
Err(error) => handle_error(
&config,
error,
false,
),
};

let errors = top_level_command.validate();
for error in &errors {
println!("{}: {}", error.0.display(), print_error(error.1.clone()));
}

if errors.is_empty() {
exit(0);
} else {
exit(1);
}
}

let user_cli_command = config.user_cli_command(&config.name);
let user_cli_args = parse_user_cli_args(&user_cli_command, sub_cli_args.cliargs);

Expand Down Expand Up @@ -88,6 +66,18 @@ fn main() {
Ok(code) => exit(code),
Err(error) => handle_error(&config, error, true),
},
UserCliMode::Validate => {
let errors = subcommand.validate();
for error in &errors {
println!("{}: {}", error.0.display(), print_error(error.1.clone()));
}

if errors.is_empty() {
exit(0);
} else {
exit(1);
}
}
}
}

Expand Down Expand Up @@ -168,10 +158,6 @@ struct SubCli {
#[command(flatten)]
exec_and_rel: ExecutableAndRelative,

#[arg(long)]
#[arg(help = "Validate that the CLI is correctly configured")]
validate: bool,

#[arg(help = "Arguments to pass to the CLI", raw = true)]
cliargs: Vec<String>,
}
Expand Down Expand Up @@ -199,7 +185,6 @@ struct SubCliArgs {
color: Color,
root: PathBuf,
infer_long_arguments: bool,
validate: bool,
cliargs: Vec<String>,
}

Expand All @@ -210,6 +195,7 @@ enum UserCliMode {
Help,
Commands(Option<String>),
Completions,
Validate,
}

struct UserCliArgs {
Expand All @@ -233,6 +219,8 @@ fn parse_user_cli_args(cmd: &Command, cliargs: Vec<String>) -> UserCliArgs {
UserCliMode::Help
} else if args.get_one::<bool>("commands").cloned().unwrap_or(false) {
UserCliMode::Commands(args.get_one::<String>("extension").cloned())
} else if args.get_one::<bool>("validate").cloned().unwrap_or(false) {
UserCliMode::Validate
} else if args
.get_one::<bool>("completions")
.cloned()
Expand Down Expand Up @@ -279,8 +267,6 @@ fn parse_sub_cli_args() -> SubCliArgs {

infer_long_arguments: args.infer_long_arguments,

validate: args.validate,

cliargs: args.cliargs,

root,
Expand Down

0 comments on commit d049ffb

Please sign in to comment.