getopts

Using getopt_long (C++) how do I code up a long & short option to both require arguments?

痞子三分冷 提交于 2019-12-04 12:47:32
#include <iostream> #include <getopt.h> #define no_argument 0 #define required_argument 1 #define optional_argument 2 int main(int argc, char * argv[]) { std::cout << "Hello" << std::endl; const struct option longopts[] = { {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {"stuff", required_argument, 0, 's'}, {0,0,0,0}, }; int index; int iarg=0; //turn off getopt error message opterr=1; while(iarg != -1) { iarg = getopt_long(argc, argv, "svh", longopts, &index); switch (iarg) { case 'h': std::cout << "You hit help" << std::endl; break; case 'v': std::cout << "You hit version" <

getopts won't call twice in a row? [duplicate]

淺唱寂寞╮ 提交于 2019-12-04 02:13:00
This question already has an answer here: Why does getopts only work the first time? 1 answer For some reason the options work fine the first call of lib_progress_bar -c "@" -u "_" 0 100 , but on the second call and beyond everything is default because it seems like getopts c:u:d:p:s:%:m: flag isn't true the second time around, or atleast the case is never executed when I used set -x #!/bin/bash lib_progress_bar() { local current=0 local max=100 local completed_char="#" local uncompleted_char="." local decimal=1 local prefix=" [" local suffix="]" local percent_sign="%" local max_width=$(tput

BASH: getopts with default parameters value

血红的双手。 提交于 2019-12-02 09:21:35
I've got another bash-script problem I simply couldn't solve. It's my simplified script showing the problem: while getopts "r:" opt; do case $opt in r) fold=/dev dir=${2:-fold} a=`find $dir -type b | wc -l` echo "$a" ;; esac done I calling it by: ./sc.sh -r /bin and it's work, but when I don't give a parameter it doesn't work: ./sc.sh -r I want /dev to be my default parameter $2 in this script. There may be other parameters before, don't hard-code the parameter number ($2). The getopts help says When an option requires an argument, getopts places that argument into the shell variable OPTARG. .

How can I use long options with the Bash getopts builtin?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 04:47:57
问题 I am trying to parse a -temp option with Bash getopts. I'm calling my script like this: ./myscript -temp /foo/bar/someFile Here is the code I'm using to parse the options. while getopts "temp:shots:o:" option; do case $option in temp) TMPDIR="$OPTARG" ;; shots) NUMSHOTS="$OPTARG" ;; o) OUTFILE="$OPTARG" ;; *) usage ;; esac done shift $(($OPTIND - 1)) [ $# -lt 1 ] && usage 回答1: getopts can only parse short options. Most systems also have an external getopt command, but getopt is not standard,

bash getopts with multiple and mandatory options

£可爱£侵袭症+ 提交于 2019-11-27 17:31:22
Is it possible to use getopts to process multiple options together? For example, myscript -iR or myscript -irv. Also, I have a situation where based on a condition script would need mandatory option. For example, if argument to script is a directory, I will need to specify -R or -r option along with any other options (myscript -iR mydir or myscript -ir mydir or myscript -i -r mydir or myscript -i -R mydir), in case of file only -i is sufficient (myscript -i myfile). I tried to search but didn't get any answers. You can concatenate the options you provide and getopts will separate them. In your

Best way to parse command line args in Bash?

旧巷老猫 提交于 2019-11-27 08:28:46
After several days of research , I still can't figure out the best method for parsing cmdline args in a .sh script. According to my references the getopts cmd is the way to go since it " extracts and checks switches without disturbing the positional parameter variables.Unexpected switches, or switches that are missing arguments, are recognized and reportedas errors. " Positional params(Ex. 2 - $@, $#, etc) apparently don't work well when spaces are involved but can recognize regular and long parameters(-p and --longparam). I noticed that both methods fail when passing parameters with nested

Retrieving multiple arguments for a single option using getopts in Bash

泪湿孤枕 提交于 2019-11-27 03:53:24
I need help with getopts . I created a Bash script which looks like this when run: $ foo.sh -i env -d directory -s subdirectory -f file It works correctly when handling one argument from each flag. But when I invoke several arguments from each flag I am not sure how to pull the multiple variable information out of the variables in getopts . while getopts ":i:d:s:f:" opt do case $opt in i ) initial=$OPTARG;; d ) dir=$OPTARG;; s ) sub=$OPTARG;; f ) files=$OPTARG;; esac done After grabbing the options I then want to build directory structures from the variables foo.sh -i test -d directory -s

Using getopts inside a Bash function

偶尔善良 提交于 2019-11-27 00:19:49
I'd like to use getopts inside a function that I have defined in my .bash_profile. The idea is I'd like to pass in some flags to this function to alter its behavior. Here's the code: function t() { echo $* getopts "a:" OPTION echo $OPTION echo $OPTARG } When I invoke it like this: t -a bc I get this output: -a bc ? What's wrong? I'd like to get the value bc without manually shifting and parsing. How do I use getopts correctly inside a function? EDIT: corrected my code snippet to try $OPTARG, to no avail EDIT #2: OK turns out the code is fine, my shell was somehow messed up. Opening a new

Best way to parse command line args in Bash?

送分小仙女□ 提交于 2019-11-26 14:09:11
问题 After several days of research, I still can't figure out the best method for parsing cmdline args in a .sh script. According to my references the getopts cmd is the way to go since it " extracts and checks switches without disturbing the positional parameter variables.Unexpected switches, or switches that are missing arguments, are recognized and reportedas errors. " Positional params(Ex. 2 - $@, $#, etc) apparently don't work well when spaces are involved but can recognize regular and long

Retrieving multiple arguments for a single option using getopts in Bash

风流意气都作罢 提交于 2019-11-26 11:00:15
问题 I need help with getopts . I created a Bash script which looks like this when run: $ foo.sh -i env -d directory -s subdirectory -f file It works correctly when handling one argument from each flag. But when I invoke several arguments from each flag I am not sure how to pull the multiple variable information out of the variables in getopts . while getopts \":i:d:s:f:\" opt do case $opt in i ) initial=$OPTARG;; d ) dir=$OPTARG;; s ) sub=$OPTARG;; f ) files=$OPTARG;; esac done After grabbing the