Optparse is a Bash library using getopts to help options parsing. It supports not only the short option, but also the long option name, target variable name, value and default value, and help description.
With the following partial sample code from the project, you can see its potential:
# Source the optparse.bash file --------------------------------------------------- source optparse.bash # Define options optparse.define short=f long=file desc="The file to process" variable=file optparse.define short=o long=output desc="The output file" variable=output default=head_output.txt optparse.define short=l long=lines desc="The number of lines to head (default:5)" variable=lines default=5 optparse.define short=v long=verbose desc="Flag to set verbose mode on" variable=verbose_mode value=true default=false # Source the output file ---------------------------------------------------------- source $( optparse.build )
It can generate a help text as follows:
$ ./sample_head.sh -? usage: ./sample_head.sh [OPTIONS] OPTIONS: -f --file: The file to process -o --output: The output file [default:head_output.txt] -l --lines: The number of lines to head (default:5) [default:5] -v --verbose: Flag to set verbose mode on [default:false] -? --help : usage
To use it as the following mixed cases of short and long option names, it’d be like:
$ ./sample_head.sh -l 3 -f sample_head.sh --output /dev/stdout #!/usr/bin/env bash # Source the optparse.bash file ---------------------------------------------------
It’s written in Bash, of course, by Nagarjuna Kumar. I don’t see any license mentioned at this moment, you may want to ask before you really get to use it for your projects.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.