https://docs.python.org/3/library/argparse.html is great.
Powershell has the Param keyword that functions like argparse in Python
https://docs.microsoft.com/en-us/powershell/module/microsoft...
https://docs.python.org/3/library/argparse.html is great.
Powershell has the Param keyword that functions like argparse in Python
https://docs.microsoft.com/en-us/powershell/module/microsoft...
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
do_help
exit
;;
-v|--version)
do_version
exit
;;
-d|--debug)
debug=true
shift
;;
-a|--arg)
arg_value=$2
shift 2
;;
esac
done while (( $# )); do
case "$1" in
-h|--help)
usage
exit
;;
-v|--version)
do_version
exit
;;
-d|--debug)
debug=true
;;
-a|--arg)
arg_value="$2"
shift
;;
*)
if [[ ! -v pos1 ]]; then
pos1="$1"
elif [[ ! -v pos2 ]]; then
pos2="$1"
else
>&2 printf "%s: unrecognized argument\n" "$1"
>&2 usage
exit 1
fi
esac
shift
done foo -da bar
whereas getopts does. On the other hand, with (the Bash built-in) getopts you're limited to single character flags. while (( $# )); do
case "$1" in
-*h*|--help)
do_help
exit
;;
-*v*|--version)
do_version
exit
;;
-*d*|--debug)
debug=true
;;&
-*a*|--arg)
value="$2"
shift
;;&
esac
shift
done
It doesn't support args of the form -avalue but those a pretty uncommon anyway. -*a*|--arg)
You could fix that with: -a*|-[!-]*a*|--arg)
> It doesn't support args of the form -avalue but those a pretty uncommon anyway.You could
-a*|-[!-]*a*|--arg)
if [[ "$1" != --arg ]]; then
value="${1#*a}"
fi
if [[ ! "$value" ]]; then
value="$2"
shift
fi
;;&
Putting the option stuck together to its value has the advantage of working nicely with brace expansion. For example, you can call `strace -p{1111,2222,3333}` to trace those 3 pids and avoid having to type `-p` 3 times. -d*|-[!-]*d*|--debug)
if [[ ! "$finished_case" && ("$1" = --debug || "$1" =~ '^[^ad]*d') ]]; then
debug=true
fi
;;&
-a*|-[!-]*a*|--arg)
if [[ ! "$finished_case" && ("$1" = --arg || "$1" =~ '^[^a]*a') ]]; then
if [[ "$1" != --arg ]]; then
value="${1#*a}"
fi
if [[ ! "$value" ]]; then
value="$2"
shift
fi
finished_case=true
fi
;;&
...
esac
shift
finished_case=
done
All case-clauses would need to use `;;&` by the way, including `-v` and `-h`. The regex is generally: "^[^${all_options_with_values}${current_option}]*${current_option}"
Another problem is that option and argument non-recognition would not work as previously layed out. You can include short options that aren't recognized, and they'll be ignored instead of raising errors. For positional arguments, one would need a condition to check for options, since using `;;&` for everything means that everything would land to *)
Maybe those are the last issues, but this is already out of hand for otherwise small and simple shell scripts. All these complications arise from trying to support the sticking together of short options and their possible values. Processing arguments in a case loop is much, much simpler if we avoid supporting those 2 features. import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v','--version',action='version', version='demo',help='Print version information')
parser.add_argument('-d','--debug', help='Enable Debug Mode')
parser.add_argument('a','arg', help="Argument Documentation")
args = parser.parse_args()
Personally I feel like this is more readable code, gets me better validation, and help docs for "free". That's the attraction.If you can live with additional dependencies, then I like the node [1] commander package, which is very readable and nice to work with in my opinion.
#!/usr/bin/env node
const { program } = require('commander');
program
.command('clone <source> [destination]')
.description('clone a repository')
.action((source, destination) => {
console.log('clone command called');
});
It also automatically generates the --help output for ./script -hArgparse is okay (and being in stdlib makes it always-available), but it's no click. https://click.palletsprojects.com/en/7.x/
Is that really reasonable sounding to most people for parsing CLI input/output and display manpages or helptext?
The case I used it for was much more complex. What I liked about it was the easy to use API, clear documentation & examples, and readable patterns.
For simple text display, I like the solution from the article, and I learned something new about bash scripts. Also, I learned from comments you can use heredoc in bash!
A great option when you're stuck with an old crusty script that you don't want to completely rewrite in python, but do want to clean up enough so that you can call it with `-h` and remember how to use it a few months in the future.
Unfortunately, this won't help you if you're on embedded where python isn't in the base system.
https://docs.microsoft.com/en-us/powershell/module/microsoft...