Validating command line arguments in shell script city phone dating
' 'optional-arg' 'Some optional arg with a default' 'default_value' cmdarg 'a:' 'required-validated-arg' 'Some required argument with a validator' '' validator_function The name of a bash function which will validate this argument (see VALIDATORS below).Validators must be bash function names - not bash statements - and they must accept one argument, being the value to validate.Similarly, cmdarg understands '--' which means "stop processing arguments, the rest of this stuff is just to be passed to the program directly". Cmdarg would parse -x and --longopt as expected, and then $ would hold "--some-thing-with-dashes", for your program to do with what it will.cmdarg is meant to be extensible by default, so there are some places where you can hook into it to change cmdarg's behavior.Any REQUIRED argument that is not specified is considered a failure.However, it is worth noting that if a required argument has a default value, and you provide an empty value to it, we won't know any better and that will be accepted (how do we know you didn't actually to do that? For every argument integer, boolean or string argument, a global associative array "cmdarg_cfg" is populated with the long version of the option.
Like any good option parsing framework, cmdarg understands '--' and positional arguments that are meant to be provided without any kind of option parsing applied to them. It would seem reasonable to assume that -x and --longopt would be parsed as expected; with arguments of 0 and thingy. cmdarg puts those into a bash indexed array called cmdarg_argv.
You needn't limit your use of this method to running scripts either, although that is where it's most useful.
The following script, named When you write a shell script, arguments are commonly needed for it to function properly.
cmdarg is a helper library I wrote for bash scripts because, at current, option parsing in bash (-foo bar, etc) is really hard, lots harder than it should be, given bash's target audience. There are 4 functions you will care about: Cmdarg lets you specify arguments (things you require), options (things you don't require), and lets you easily parse them.
The arguments can be set on the command line either via '-X' or '--Y', where X is the short option and Y is the long option.
In order to ensure that those arguments make sense, it's often necessary to validate them.