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
19
votes
3 answers

How can I allow undefined options when parsing args with Getopt

If I have a command line like: my_script.pl -foo -WHATEVER My script knows about --foo, and I want Getopt to set variable $opt_foo, but I don't know anything about -WHATEVER. How can I tell Getopt to parse out the options that I've told it about,…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
18
votes
10 answers

Can OptionParser skip unknown options, to be processed later in a Ruby program?

Is there any way to kick off OptionParser several times in one Ruby program, each with different sets of options? For example: $ myscript.rb --subsys1opt a --subsys2opt b Here, myscript.rb would use subsys1 and subsys2, delegating their options…
inger
  • 19,574
  • 9
  • 49
  • 54
18
votes
5 answers

Getopt not included? implicit declaration of function ‘getopt’

I wanted to use getopt, but it just won't work. It's giving me gcc -g -Wall -std=c99 -ftrapv -O2 -Werror -Wshadow -Wundef -save-temps -Werror-implicit-function-declaration -c -o src/main.o src/main.c src/main.c: In function…
MightyPork
  • 18,270
  • 10
  • 79
  • 133
18
votes
5 answers

getopt fails to detect missing argument for option

I have a program which takes various command line arguments. For the sake of simplification, we will say it takes 3 flags, -a, -b, and -c, and use the following code to parse my arguments: int c; while((c = getopt(argc, argv, ":a:b:c")) !=…
finiteloop
  • 4,444
  • 8
  • 41
  • 64
16
votes
6 answers

Trapping getopt invalid options

I'm using getopt (not getops) to provide the ability for my bash script to process options and switches (both long --option and short -o forms). I'd like to be able to trap invalid options and handle them, typically echoing out that the user should…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
15
votes
3 answers

Passing newline within string into a python script from the command line

I have a script that I run from the command line which I would like to be able to pass string arguments into. As in script.py --string "thing1\nthing2" such that the program would interpret the '\n' as a new line. If string="thing1\nthing2" I want…
Astro_Dart
  • 195
  • 1
  • 1
  • 9
14
votes
2 answers

Perl GetOptions() case sensitivity

GetOptions( "r|repo=s" => \$repo, "R|list-repos" => \$list, ); When I call this script with -r qwe option, $list is updated to 1, which is not what I expect. How can I make GetOpt case sensitive?
mateusza
  • 5,341
  • 2
  • 24
  • 20
14
votes
4 answers

Getopt optional arguments?

I have a program where you enter an option -d and then whether or not you supply a non-optional argument after the option, do something. Heres my code: #include #include #include #define OPT_LIST "d::" int main…
pudumaster
  • 161
  • 1
  • 1
  • 4
13
votes
1 answer

getopt_long_only segmentation fault

I'm trying to use getopt_long_only to parse a command line. My app reads a handful of command line options. E.g. "app --alpha=1 --beta=2 --cecil=3" getopt_long_only works fine as expected as long as valid command line parameters are passed in. …
selbie
  • 100,020
  • 15
  • 103
  • 173
13
votes
1 answer

Mandatory parameter getopt in C

I have this piece of code in C while((i = getopt(argc, argv, ":p:h:s:n:l:f:SLNF")) != -1) switch(i){ case 'p': printf("Porta obbligatoria\n"); break; case 'h': printf("hostname\n"); break; …
Pupi
  • 131
  • 1
  • 1
  • 5
13
votes
1 answer

Is there a way to "reset" getopt for non-global use?

When attempting to use getopt multiple times, the error I get in valgrind is Invalid read of size 1. The error only occurs when doing something like this: ls -a -b ls -a -b Therefore I'm assuming the problem is with reusing the getopt…
user1508519
12
votes
3 answers

What's the difference between argp and getopt?

I think the title is self explanatory. I am making a program and I was wondering what I should use of the two and why.
Pithikos
  • 18,827
  • 15
  • 113
  • 136
12
votes
7 answers

C++ and command line options

Is it bad form to use the GNU getopt in C++ programs? Is there a C++ specific alternative, or should I still just use getopt?
oadams
  • 3,019
  • 6
  • 30
  • 53
12
votes
2 answers

What is the alternative to getopt function on Windows c++?

The code below I'm using Posix C: while ((opt = getopt(argc, argv, "a:p:h")) != -1) How can I port this code to Windows C++ using an alternative function? Thanks
12
votes
4 answers

Cross-platform getopt for a shell script

I've just found out that getopt is not cross-platform (in particular for FreeBSD and Linux). What is the best workaround for this issue?
codeholic
  • 5,680
  • 3
  • 23
  • 43
1
2
3
47 48