nk412 / optparse Goto Github PK
View Code? Open in Web Editor NEWSimple command line arguments parser for BASH
License: MIT License
Simple command line arguments parser for BASH
License: MIT License
It seems only courteous to alert the maintainers of this project to https://unix.stackexchange.com/q/253481/135943 which specifically asks about one of the pieces of code in it.
The -- stopper is not recognized to stop parsing arguments
Test:
#!/bin/bash
. optparse.bash
optparse.define short=t long=test desc='Test run' variable=TEST value=1 default=0
source $( optparse.build )
echo "$@"
Result:
# ./t.sh -t -- arg1 arg2
Unrecognized long option: --
usage: ./t.sh [OPTIONS]
...
I was trying the sample_head.sh
and got four of this:
optparse.bash: line 60: rpad: command not found
I don't have rpad
command on my system, which is Gentoo and I don't think it's available to Gentoo since I couldn't search any packages include that filename. And I couldn't find the real source so I could install it, either.
Anyway, judging by its name and the problematic line 60:
optparse_usage="${optparse_usage}#NL#TB${short} $(rpad "$long:" 25) ${desc}"
Since this project is using Bash, I think just using printf
:
printf -v optparse_usage "%s#NL#TB%s %-25s %s" "$optparse_usage" "$short" "$long:"$desc"
Like I said I don't have rpad
, so I couldn't test it with real a run, but I believe it should give exactly the same result.
optparse.define short=A long=extraDockerRunArgs desc="optional, quoted string of extra docker run argument flags/values" variable=EXTRA_DOCKER_RUN_ARGS default=""
mycmd.sh -A "--memory=\"256M\" --cpu-shares=\"100\""
results in
Unrecognized long option: --memory="256M" --cpu-shares="100"
The optparse.bash code has a mix of tabs an spaces. Please stick to one. ;-)
At the moment, no license is specified. This typically means that the code is copyrighted by the author (e.g. you) and cannot be used by anyone else without explicit permission from the author. That's a pity, because optparse seems useful, and I'm sure people would like to be able to use it without any hassle.
Please could you consider licensing optparse under the AGPLv3, or at least under a free software license that is compatible with it? That will enable other people to use optparse in their own projects, without having to write to you for permission each time.
Thanks!
Cloning the repo and running $ bash sample_head.sh --help
produces the following output, including a syntax error:
$ bash sample_head.sh --help
optparse.bash: line 140: #NL: syntax error: operand expected (error token is "#NL")
sample_head.sh: line 11: source: filename argument required
source: usage: source filename [arguments]
ERROR: Please provide a file
Looks like this associative array assignment is throwing the error:
local -A o=( ['#NL']='\n' ['#TB']='\t' )
Running the following BASH version:
$ bash --version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.
Any idea how to fix this? My bash foo is pretty weak.
I see the use of #NL
and #TB
as placeholders for newlines and tabs (respectively), which are replaced on the lines following that assignment. It looks like the use of #
as a special character is the reason for the ['#NL']
syntax in the array declaration. Not really sure how that works or how to debug this.
I'm packaging this project for the Nix package manager's Nixpkgs repo. Packages that doesn't have a version pinned using git tag or releases are treated differently there.
Besides, it would be easier to decide if the source code should be updated.
Ubuntu 13.10 does not have rpad. Please see my fork for the fix.
- optparse_usage="${optparse_usage}#NL#TB${short} $(rpad "$long:" 25) ${desc}"
optparse_usage="${optparse_usage}#NL#TB${short} $(printf "%-25s %s" "${long}:" "${desc}")"
Eg; ./zkcomp (without passing any arguments, it doesn't validate options)
on occasion i need to reparse the options so we would need to without the defaults.
command -c config_file -i override
where it would read the config file first then reparse the args to override environment variable.
calling the code would look something like
source $( optparse.build )
if [ -n "$CONFIG_FILE" ]; then
source $CONFIG_FILE_PATH
# and reparsing
source $( optparse.build "DEFAULT_OFF")
fi
and there needs to be stop gate like
# Set default variable values
if [[ "$*" == *DEFAULT_OFF* ]]; then
echo -n ""
else
$optparse_defaults
fi
# Process using getopts
OPTIND=1
while getopts "$optparse_arguments_string" option; do
case \$option in
...
(note: OPTIND=1 )
--with-default-names
no longer works, this is gone in brew, please provide a solution
The files under /tmp/optparse-${RANDOM}.tmp are not cleaned up. Use 'bash' traps to do the cleanup: http://stackoverflow.com/questions/687014/removing-created-temp-files-in-unexpected-bash-exit/687033#687033
can this support more than 26 options? I.E. the limit of possibilities for the -short
option, does that take anything other than alphas?
Here's an example:
$ ./a.sh -v -L wfrge
Would get:
scp -P22334 [email protected]:/opt/scripts/db/.backup
To:
/vagrant/db/.backup
$ ./a.sh -v 1 -L wfrge
SSH login is required option.
For help invoke with -? or --help
The definitions of the options is as the following:
optparse.define short=L long=ssh-login desc="The login to use for sftp/scp connection" variable=ssh_login
optparse.define short=v long=verbose desc="" desc="Verbose mode." variable=verbose value=1 default=0
Works same way if long option name specified.
The arguments help is differerent aligned than the "default" help description.
Test:
#!/bin/bash
. optparse.bash
optparse.define short=T long=test desc='Test run' variable=TEST default=false
source $( optparse.build )
echo "$@"
Result:
# ./t.sh --help
usage: ./t.sh [OPTIONS]
OPTIONS:
-T --test: Test run [default:false]
-? --help : usage
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.