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
6
votes
4 answers

Adding a help command to a script

Is there a standard way of adding a help function to a script? The simplest way would maybe to take an argument and print some text if it's "-help" or something. Does anyone have any examples on how to do this? Thanks!
6
votes
2 answers

Unable to parse command line long options

#!/usr/bin/perl -sw use strict; use warnings; use Getopt::Long; my $remote = 0; my $test = 0; GetOptions ('remote' => \$remote, 'test' => \$test); print "$remote:$test\n"; perl test.pl --remote --test The above prints "0:0". I am new to Perl so I…
scott.smart
  • 538
  • 5
  • 16
6
votes
2 answers

Using Perl's Getopt::Long, how can I prevent the module from trying to match ambiguous option names?

I am using the Getopt::Long module to process command line arguments. The typical behavior of this module is we could pass -f instead of full name of the variable --file. At the same time if I have another command line variable --find, and if I…
Gentle
  • 197
  • 8
5
votes
4 answers

How do I use getopt_long to parse multiple arguments?

#include #include #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[] = { …
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
5
votes
1 answer

How can I access a Getopt::Long option's value in the option's sub?

My goal is to have a --override=f option that manipulates the values of two other options. The trick is figuring out how to refer to the option's value (the part matching the f in the =f designator) in the sub that's executed when GetOptions detects…
Cary Millsap
  • 806
  • 6
  • 17
5
votes
1 answer

Mandatory options with getopt_long() in C

With C/ C++, getopt_long() can be used to parse command line arguments. Is it possible to tell the function that some of the options are mandatory? For example, how can I tell getopt_long that the parsing should give error if startServer is called…
Jaywalker
  • 3,079
  • 3
  • 28
  • 44
5
votes
2 answers

why struct option array needs an addtional dummy entry when using getopt_long

For example the option array is: static struct option const long_options[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; Is it for padding?
Oxdeadbeef
  • 1,033
  • 2
  • 11
  • 26
5
votes
1 answer

How do I make getopt in C++ do option checking strictly?

I am using getopt to parse inputs for a CLI written in C++. I have long and short options and my struct long_options[] element looks like this: {"verbose", no_argument, NULL, "v"} One observation is - on the command line, even if I pass #…
Ravindra Mijar
  • 1,282
  • 2
  • 9
  • 17
5
votes
1 answer

Why gdb displays optarg as 0x0 all the time

I am learning how getopt and *getopt_long* work. One problem is that when I use gdb to run the following simple program step by step, the optarg is always 0x0. Do you know why?Is it the problem of gdb? I tried to search the web and look at the…
Duke
  • 1,332
  • 12
  • 12
4
votes
2 answers

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

#include #include #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[] = { …
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
4
votes
1 answer

Detecting ambiguous options with Getopt::Long

Is there an easy way to detect ambiguous options with the Perl module Getopt::Long? For example: #!/usr/bin/env perl # test ambiguous options use Getopt::Long; my $hostname = 'localhost'; GetOptions( help => sub { print "call usage sub…
vlee
  • 1,369
  • 3
  • 14
  • 23
4
votes
3 answers

How to use GetOptions utility to handle 'optional' command-line arguments in Perl?

There are many Perl tutorials explaining how to use GetOptions utility to process only the command-line arguments which are expected, else exit with an appropriate message. In my requirement I have following optional command-line arguments, like,…
TheCottonSilk
  • 8,662
  • 2
  • 26
  • 37
4
votes
6 answers

Can Perl's Getopt::Long parse arguments I don't define ahead of time?

I know how to use Perl's Getopt::Long, but I'm not sure how I can configure it to accept any "--key=value" pair that hasn't been explicitly defined and stick it in a hash. In other words, I don't know ahead of time what options the user may want, so…
accelerate
  • 1,215
  • 3
  • 16
  • 30
4
votes
1 answer

How to get the value of an option which begins with '+' character?

I am writing an option parser for a bash-like shell I develop. Nevertheless, to be compatible with bash options, I must read some options which begin with a '+', like this: ./42sh +O autocd [...] (The manual page says these options will passed to…
4
votes
3 answers

Add Getopt::Long options in a hash, even when using a repeat specifier

Perl's Getopt::Long allows a developer to add their own options to a script. It's also possible to allow multiple values for an option by the use of a repeat specifier, as seen in regular expressions. For example: GetOptions('coordinates=f{2}' =>…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
1
2
3
12 13