diff --git a/README.md b/README.md index 4a5b014..2435428 100644 --- a/README.md +++ b/README.md @@ -18,22 +18,22 @@ A plain argument such as `arg` is a required argument, which will be passed to t An argument starting with `$` such as `$arg` is an optional argument, which will be passed to the script as an environment variable like `$ARGSH_ARG`, where all `-` get converted to `_`. +An argument starting with `?` such as `?arg` is a flag, which will be passed to the script as an environment variable like `$ARGSH_ARG`, where all `-` get converted to `_`, with the value set to `1`, the value is irrelevant, just check if the variable is empty. + Any additional positional arguments passed to the script get appended after the required arguments. -Optionally, you can set the shell to use as such `bash|name;count`, defaults to `sh` if ommited. +Optionally, you can set the shell to use as such `bash|name;count;etc`, defaults to `sh` if ommited. ## Limitations For the short version of arguments to work properly, all arguments must start with different characters. -All arguments must have a matching value, as such, flags aren't possible using argsh. - -Limit of 256 arguments (if you have more than that please use an actual programming language). +Limit of 256 required arguments (if you have anything close to that please use an actual programming language). ## Testing Run the test script as such and mess around with the arguments ``` -./test --required indeed --also-required nice --optional woah --second-optional crazy am additional +./test --required indeed --also-required nice --flag --optional woah --second-optional crazy am additional ``` diff --git a/src/main.rs b/src/main.rs index a9a8fbd..2726dac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ mod config { pub enum Arg { Required(String, u8), Optional(String), + Flag(String), } fn is_valid_name(s: &str) -> bool { @@ -62,6 +63,12 @@ mod config { } else { return Err("Invalid argument name".into()); } + } else if let Some(arg) = arg.strip_prefix('?') { + if is_valid_name(arg) { + args.push(Arg::Flag(arg.to_owned())); + } else { + return Err("Invalid argument name".into()); + } } else { if is_valid_name(arg) { pos += 1; @@ -82,6 +89,7 @@ mod config { self.args.iter().find(|arg| match arg { Arg::Required(arg, _) => arg == arg_to_match, Arg::Optional(arg) => arg == arg_to_match, + Arg::Flag(arg) => arg == arg_to_match, }) } @@ -91,6 +99,7 @@ mod config { arg.chars().nth(0).map_or(false, |arg| arg == arg_to_match) } Arg::Optional(arg) => arg.chars().nth(0).map_or(false, |arg| arg == arg_to_match), + Arg::Flag(arg) => arg.chars().nth(0).map_or(false, |arg| arg == arg_to_match), }) } } @@ -112,14 +121,20 @@ fn execute( match arg { Short(short) => { if let Some(arg) = config.match_arg_short(short) { - args.insert(arg, parser.value()?.parse()?); + match arg { + config::Arg::Flag(_) => args.insert(arg, String::new()), + _ => args.insert(arg, parser.value()?.parse()?), + }; } else { return Err(arg.unexpected()); } } Long(long) => { if let Some(arg) = config.match_arg(long) { - args.insert(arg, parser.value()?.parse()?); + match arg { + config::Arg::Flag(_) => args.insert(arg, String::new()), + _ => args.insert(arg, parser.value()?.parse()?), + }; } else { return Err(arg.unexpected()); } @@ -130,6 +145,7 @@ fn execute( let mut required_args: BTreeMap = BTreeMap::new(); let mut optional_args: Vec<(String, String)> = Vec::new(); + let mut flags: Vec = Vec::new(); for (arg, value) in args { match arg { @@ -142,11 +158,15 @@ fn execute( value, )); } + config::Arg::Flag(arg) => { + flags.push(format!("ARGSH_{}", arg.replace('-', "_").to_uppercase())); + } } } let error = std::process::Command::new(config.shell.unwrap_or("sh".to_owned())) .envs(optional_args) + .envs(flags.iter().map(|flag| (flag, "1".to_string()))) .arg(script) .args(required_args.into_values()) .args(pos_args) diff --git a/test b/test index 9e1babc..783bbd6 100755 --- a/test +++ b/test @@ -1,8 +1,14 @@ -#!./target/debug/argsh bash|required;also-required;$optional;$second-optional +#!./target/debug/argsh bash|required;also-required;?flag;$optional;$second-optional echo "--required on pos 1: $1" echo "--also-required on pos 2: $2" +if [[ -n "$ARGSH_FLAG" ]]; then + echo "--flag: passed" +else + echo "--flag: not passed" +fi + if [[ -n "$ARGSH_OPTIONAL" ]]; then echo "--optional: $ARGSH_OPTIONAL" else