Questions tagged [getopt-long]

Getopt::Long is a command line switch parsing library for Perl. For C-programming: The getopt and getopt_long functions automate some of the chores involved in parsing typical unix command line options.

The Getopt::Long Perl module is a parser for the POSIX command line switch syntax with GNU extensions. Both the traditional -s single character switches and the longer --longer switches are supported.

For C-programming: The getopt and getopt_long functions automate some of the chores involved in parsing typical unix command line options. For details, please see here.

190 questions
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
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
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
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
14
votes
2 answers

Where does getopt_long store an unrecognized option?

When getopt or getopt_long encounters an illegal option, it stores the offending option character in optopt. When the illegal option is a long option, where can I find out what the option was? And does anything meaningful get stored in optopt…
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
10
votes
1 answer

C++ boost::program_options reading arguments compatible with getopt_long

I'm developing an update in an existing program. I'm replacing Posix's getopt_long() with boost::program_options. But my work doesn't work as I should: I want to have read arguments like: -server=www.example.com -c config.txt I was trying many…
baziorek
  • 2,502
  • 2
  • 29
  • 43
8
votes
5 answers

Is there anyway to persuade python's getopt to handle optional parameters to options?

According to the documentation on python's getopt (I think) the options fields should behave as the getopt() function. However I can't seem to enable optional parameters to my code: #!/usr/bin/python import sys,getopt if __name__ == "__main__": …
stsquad
  • 5,712
  • 3
  • 36
  • 54
8
votes
1 answer

Should you check the return code from Getopt::Long::GetOptions?

I've just been asked for the first time in a code review to check the return code from a call to the GetOptions() function of the Getopt::Long Perl module. I cannot remember ever seeing such a test for the GetOptions() function. So is there a…
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
7
votes
1 answer

getopt_long could not be resolved in Eclipse CDT

Using the in a C++ program, Eclipse CDT marks getopt_long as "could not be resolved". The code compiles and runs fine using g++ program.cpp. Has this something to do with the Eclipse build set up?
Sash
  • 4,448
  • 1
  • 17
  • 31
7
votes
1 answer

Understanding `option long_options[]` when using `getopt_long`

I am trying to learn to use getopt_long. From wikipedia, I see the code #include /* for printf */ #include /* for exit */ #include /* for getopt_long; POSIX standard getopt is in unistd.h */ int main (int…
Remi.b
  • 17,389
  • 28
  • 87
  • 168
7
votes
5 answers

How do I parse a string with GetOpt::Long::GetOptions?

I have a string with possible command line arguments (using an Read-Eval-Print-Loop program) and I want it to be parsed similar to the command line arguments when passed to Getopt::Long. To elaborate: I have a string $str = '--infile…
Jagmal
  • 5,726
  • 9
  • 35
  • 35
7
votes
1 answer

Avoiding mix of certain arguments to script

I have a script which can get tens of arguments/flags using Getopt::Long. Certain flags are not allowed to be mixed, such as: --linux --unix are not allowed to be supplied together. I know I can check using an if statement. Is there is a cleaner…
snoofkin
  • 8,725
  • 14
  • 49
  • 86
6
votes
2 answers

How to use getoptlong class in ruby?

I need help using getoptlong class in Ruby. I need to execute command prog_name.ruby -u -i -s filename. So far I can only execute it with prog_name.ruby -u filename -i filename -s filename. This is my getoptlong code: require 'getoptlong' class…
muka
  • 101
  • 1
  • 4
6
votes
3 answers

Turn off abbreviation in getopt_long (getopt.h)?

Is it possible to turn off abbreviation in getopt_long()? From the man page: Long option names may be abbreviated if the abbreviation is unique or is an exact match for some defined option. I want to do this because the specification I have…
Codeape
  • 288
  • 3
  • 14
6
votes
1 answer

Why does my Perl program print the help message when an arguments has 0 as a value?

If i do this: GetOptions( 'u=s' => \$in_username, 'r=i' => \$in_readonly, 'b=i' => \$in_backup ); exit usage() unless $in_username && $in_readonly && $in_backup; and call the program like this: ./app.pl -u david -r 12 -b 0 it…
David
  • 63
  • 2
1
2 3
12 13