4 ms·
> (e.g. "get the argument immediately after '-f'") Is there a language commonly used for cli tools that doesn't have an inbuilt options lib? Even bash has inbu
by vacri 10y ago
> (e.g. "get the argument immediately after '-f'")
Is there a language commonly used for cli tools that doesn't have an inbuilt options lib? Even bash has inbuilt 'getopts' to handle this for you.
while getopts "ab:" option; do
case $option in
a) VAR="foo";;
b) VAR2="$OPTARG";;
esac
done
shift $((OPTIND-1))
which will allow you to "myscript -a -b myarg"
By all means, use env vars if it works for you, but it should be right tool for the right job. Imagine trying to use 'ls' with env vars...
- beefhash 10y ago> Is there a language commonly used for cli tools that doesn't have an inbuilt options lib? Yes, namely C/C++ under the Visual Studio toolchain: https://stackoverflow.com/a/12689342 https://stackoverflow.com/a/12689342
- vacri 10y agoI stand corrected. I thought I might get burned by C, but didn't expect it to bring Windows in on the action :)