6 ms·
Handling of arguments is one of the reasons I reach for Python or Powershell instead of a bash script when writing my own stuff. https://docs.python.org/3/lib
by mey 6y ago
Handling of arguments is one of the reasons I reach for Python or Powershell instead of a bash script when writing my own stuff.
https://docs.python.org/3/library/argparse.html 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.powershell.core/about/about_functions_advanced_parameters?view=powershell-7 https://docs.microsoft.com/en-us/powershell/module/microsoft...
- acdha 6y agoMy rule for a long time has been that any time you have more than one screen's worth of code and/or are using any of arrays, functions, or more than simple positional arguments you'll save time by rewriting it in Python. shellcheck has helped soften that a bit but the usability difference is pretty noticeable even with multiple decades writing shell scripts.
- dragonwriter 6y ago> https://docs.python.org/3/library/argparse.html https://docs.python.org/3/library/argparse.html is great Argparse is okay (and being in stdlib makes it always-available), but it's no click. https://click.palletsprojects.com/en/7.x/ https://click.palletsprojects.com/en/7.x/
- 40four 6y agoThumbs up for 'Click'. I used it for a project once, and I was really happy with it. Easy to use, good docs. Would use it again.
- pletnes 6y agoIt’s fantastic and should be used by most CLI programs. Argparse is much faster and avoids having a dependency, so it does serve a purpose.
- gavinray 6y agoGoogling the library appears to be about ~8,000 lines of code (core.py is ~2,000 alone). Is that really reasonable sounding to most people for parsing CLI input/output and display manpages or helptext?
- dastx 6y agoI suppose it depends on the use case. Personally I've always thought argparse is good enough, and have never hit a roadblock "because I'm using argparse" so to say. Having said that, I do like the pattern click is going for. If it argparse allowed the same pattern, in my opinion that would be cool, and it would probably be my first choice.
- meddlepal 6y agoI'm not trying to be antagonistic here... but who cares how many lines it has unless you plan to maintain it?
- 40four 6y agoI didn’t mean to suggest we should reach for Click for simple help/manpage display. 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!
- mey 6y agoThis is why I love the HN community, learning something new every day. (Happy 10,000) I use argparse because it is stdlib, but will checkout click!
- Spivak 6y agoBut handling args isn't that bad in bash. 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
- jolmg 6y agoTo expand on that pattern: 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
- bewuethr 6y agoThe one downside of this is that it doesn't handle squeezing flags as in foo -da bar whereas getopts does. On the other hand, with (the Bash built-in) getopts you're limited to single character flags.
- Spivak 6y agoYou can do that it will just make things a little less pretty. 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.
- nickysielicki 6y agoSee also: https://github.com/nhoffman/argparse-bash https://github.com/nhoffman/argparse-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.
- hnarn 6y agoI also really like the "flag" package for Go, it generates the help text for you and easily lets you set defaults as well as helps you type-check inputs.
- PenguinCoder 6y agoI like https://github.com/jpillora/opts https://github.com/jpillora/opts for Go arg parsing. It has better, sane defaults with long/short opts.
- hnarn 6y agoCool, I'll check it out. There's something attractive about using stuff from the standard library though.
- daitangio 6y agoTry pyhon click: from flask& jinja2 author, it is very nice
- wahern 6y agoThe original argparse is available in every Unix shell: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/g...
- sidpatil 6y agoPowerShell also has comment-based help, which is like a manpage embedded as a comment within the script. It's like OP's suggested help format, but better. https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comment_based_help?view=powershell-7 https://docs.microsoft.com/en-us/powershell/module/microsoft...