A simple shell wrapper that handles argument parsing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add flag support

Fly b82b5a86 64545ecd

+34 -8
+5 -5
README.md
··· 18 18 19 19 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 `_`. 20 20 21 + 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. 22 + 21 23 Any additional positional arguments passed to the script get appended after the required arguments. 22 24 23 - Optionally, you can set the shell to use as such `bash|name;count`, defaults to `sh` if ommited. 25 + Optionally, you can set the shell to use as such `bash|name;count;etc`, defaults to `sh` if ommited. 24 26 25 27 ## Limitations 26 28 27 29 For the short version of arguments to work properly, all arguments must start with different characters. 28 30 29 - All arguments must have a matching value, as such, flags aren't possible using argsh. 30 - 31 - Limit of 256 arguments (if you have more than that please use an actual programming language). 31 + Limit of 256 required arguments (if you have anything close to that please use an actual programming language). 32 32 33 33 ## Testing 34 34 35 35 Run the test script as such and mess around with the arguments 36 36 37 37 ``` 38 - ./test --required indeed --also-required nice --optional woah --second-optional crazy am additional 38 + ./test --required indeed --also-required nice --flag --optional woah --second-optional crazy am additional 39 39 ```
+22 -2
src/main.rs
··· 28 28 pub enum Arg { 29 29 Required(String, u8), 30 30 Optional(String), 31 + Flag(String), 31 32 } 32 33 33 34 fn is_valid_name(s: &str) -> bool { ··· 62 63 } else { 63 64 return Err("Invalid argument name".into()); 64 65 } 66 + } else if let Some(arg) = arg.strip_prefix('?') { 67 + if is_valid_name(arg) { 68 + args.push(Arg::Flag(arg.to_owned())); 69 + } else { 70 + return Err("Invalid argument name".into()); 71 + } 65 72 } else { 66 73 if is_valid_name(arg) { 67 74 pos += 1; ··· 82 89 self.args.iter().find(|arg| match arg { 83 90 Arg::Required(arg, _) => arg == arg_to_match, 84 91 Arg::Optional(arg) => arg == arg_to_match, 92 + Arg::Flag(arg) => arg == arg_to_match, 85 93 }) 86 94 } 87 95 ··· 91 99 arg.chars().nth(0).map_or(false, |arg| arg == arg_to_match) 92 100 } 93 101 Arg::Optional(arg) => arg.chars().nth(0).map_or(false, |arg| arg == arg_to_match), 102 + Arg::Flag(arg) => arg.chars().nth(0).map_or(false, |arg| arg == arg_to_match), 94 103 }) 95 104 } 96 105 } ··· 112 121 match arg { 113 122 Short(short) => { 114 123 if let Some(arg) = config.match_arg_short(short) { 115 - args.insert(arg, parser.value()?.parse()?); 124 + match arg { 125 + config::Arg::Flag(_) => args.insert(arg, String::new()), 126 + _ => args.insert(arg, parser.value()?.parse()?), 127 + }; 116 128 } else { 117 129 return Err(arg.unexpected()); 118 130 } 119 131 } 120 132 Long(long) => { 121 133 if let Some(arg) = config.match_arg(long) { 122 - args.insert(arg, parser.value()?.parse()?); 134 + match arg { 135 + config::Arg::Flag(_) => args.insert(arg, String::new()), 136 + _ => args.insert(arg, parser.value()?.parse()?), 137 + }; 123 138 } else { 124 139 return Err(arg.unexpected()); 125 140 } ··· 130 145 131 146 let mut required_args: BTreeMap<u8, String> = BTreeMap::new(); 132 147 let mut optional_args: Vec<(String, String)> = Vec::new(); 148 + let mut flags: Vec<String> = Vec::new(); 133 149 134 150 for (arg, value) in args { 135 151 match arg { ··· 142 158 value, 143 159 )); 144 160 } 161 + config::Arg::Flag(arg) => { 162 + flags.push(format!("ARGSH_{}", arg.replace('-', "_").to_uppercase())); 163 + } 145 164 } 146 165 } 147 166 148 167 let error = std::process::Command::new(config.shell.unwrap_or("sh".to_owned())) 149 168 .envs(optional_args) 169 + .envs(flags.iter().map(|flag| (flag, "1".to_string()))) 150 170 .arg(script) 151 171 .args(required_args.into_values()) 152 172 .args(pos_args)
+7 -1
test
··· 1 - #!./target/debug/argsh bash|required;also-required;$optional;$second-optional 1 + #!./target/debug/argsh bash|required;also-required;?flag;$optional;$second-optional 2 2 3 3 echo "--required on pos 1: $1" 4 4 echo "--also-required on pos 2: $2" 5 + 6 + if [[ -n "$ARGSH_FLAG" ]]; then 7 + echo "--flag: passed" 8 + else 9 + echo "--flag: not passed" 10 + fi 5 11 6 12 if [[ -n "$ARGSH_OPTIONAL" ]]; then 7 13 echo "--optional: $ARGSH_OPTIONAL"