Questions tagged [getopt]

The `getopt` and `getopt_long` functions automate some of the chores involved in parsing typical unix command line options.

The getopt and getopt_long functions automate some of the chores involved in parsing typical unix command line options.

For details, please read here.

717 questions
524
votes
32 answers

Using getopts to process long and short command line options

I wish to have long and short forms of command line options invoked using my shell script. I know that getopts can be used, but like in Perl, I have not been able to do the same with shell. Any ideas on how this can be done, so that I can use…
gagneet
  • 35,729
  • 29
  • 78
  • 113
328
votes
5 answers

Why use argparse rather than optparse?

I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse. Why has yet another command-line parsing module been created? Why should I use it instead of…
fmark
  • 57,259
  • 27
  • 100
  • 107
58
votes
15 answers

Optional option argument with getopts

while getopts "hd:R:" arg; do case $arg in h) echo "usage" ;; d) dir=$OPTARG ;; R) if [[ $OPTARG =~ ^[0-9]+$ ]];then level=$OPTARG else level=1 fi ;; \?) echo…
iverson
  • 1,003
  • 2
  • 11
  • 14
54
votes
4 answers

getopt does not parse optional arguments to parameters

In C, getopt_long does not parse the optional arguments to command line parameters parameters. When I run the program, the optional argument is not recognized like the example run below. $ ./respond --praise John Kudos to John $ ./respond --blame…
hayalci
  • 4,089
  • 2
  • 27
  • 30
47
votes
9 answers

getopt.h: Compiling Linux C-Code in Windows

I am trying to get a set of nine *.c files (and nine related *.h files) to compile under Windows. The code was originally designed in Linux to take command line arguments using the standard GNU-Linux/C library "getopt.h". And that library does not…
john_science
  • 6,325
  • 6
  • 43
  • 60
29
votes
3 answers
29
votes
1 answer

How to support both short and long options at the same time in bash?

I want to support both short and long options in bash scripts, so one can: $ foo -ax --long-key val -b -y SOME FILE NAMES is it possible?
Lenik
  • 13,946
  • 17
  • 75
  • 103
29
votes
7 answers

Processing multiple values for one single option using getopt/optparse?

Is it possible to fetch multiple values for one option using getopt or optparse, as shown in the example below: ./hello_world -c arg1 arg2 arg3 -b arg4 arg5 arg6 arg7 Please note that the number of actual values for each option (-c, -b) could be…
Open Grieves
  • 301
  • 1
  • 3
  • 6
29
votes
8 answers

GetOpt library for C#

I'm looking for a getopt library for c#. So far I found a few (phpguru, XGetOptCS, getoptfordotnet) but these look more like unfinished attempts that only support a part of C's getopt. Is there a full getopt c# implementation?
Marijn Deé
  • 886
  • 2
  • 13
  • 21
29
votes
3 answers

How to specify an optstring in the getopt function?

I'm not sure how to correctly use optstring in the getopt function in C. How should that string be formatted? I saw examples where letters are next to each other, sometimes separated by a semicolon, sometimes by two semicolons. What does it mean?
bLAZ
  • 1,689
  • 4
  • 19
  • 31
27
votes
4 answers

After using PHP's getopt(), how can I tell what arguments remain?

OK, So PHP has a built-in getopt() function which returns information about what program options the user has provided. Only, unless I'm missing something, it's completely borked! From the manual: The parsing of options will end at the first…
edam
  • 718
  • 2
  • 8
  • 13
25
votes
7 answers

how to take integers as command line arguments?

I've read a getopt() example but it doesn't show how to accept integers as argument options, like cvalue would be in the code from the example: #include #include #include #include int main (int argc,…
Andrew
  • 1,081
  • 1
  • 8
  • 19
24
votes
6 answers

Using getopt in C with non-option arguments

I'm making a small program in C that deals with a lot of command line arguments, so I decided to use getopt to sort them for me. However, I want two non-option arguments (source and destination files) to be mandatory, so you have to have them as…
Conor Taylor
  • 2,998
  • 7
  • 37
  • 69
21
votes
5 answers

How to make a multi-character parameter in UNIX using getopt?

I'm trying to make a getopt command such that when I pass the "-ab" parameter to a script, that script will treat -ab as a single parameter. #!/bin/sh args=`getopt "ab":fc:d $*` set -- $args for i in $args do case "$i" in -ab) shift;echo…
Waffles
  • 447
  • 2
  • 5
  • 11
20
votes
5 answers

C getopt multiple value

My argument is like this ./a.out -i file1 file2 file3 How can I utilize getopt() to get 3 (or more) input files? I'm doing something like this: while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){ case 'i': input = optarg; break; …
w00d
  • 5,416
  • 12
  • 53
  • 85
1
2 3
47 48