Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:41:39+00:00 2026-06-02T13:41:39+00:00

In a C++ program, I would like to have a long-only option with a

  • 0

In a C++ program, I would like to have a “long-only” option with a required argument. Below is my minimal example using getopt_long(), but it’s not working:

#include <getopt.h>
#include <cstdlib>
#include <iostream>
using namespace std;

void help (char ** argv)
{
  cout << "`" << argv[0] << "` experiments with long options." << endl;
}

void parse_args (int argc, char ** argv, int & verbose, int & param)
{
  int c = 0;
  while (1)
  {
    static struct option long_options[] =
      {
        {"help", no_argument, 0, 'h'},
        {"verbose", required_argument, 0, 'v'},
        {"param", required_argument, 0, 0}
      };
    int option_index = 0;
    c = getopt_long (argc, argv, "hv:",
                     long_options, &option_index);
    cout << "c=" << c << endl;
    if (c == -1)
      break;
    switch (c)
    {
    case 0:
      if (long_options[option_index].flag != 0)
        break;
      printf ("option %s", long_options[option_index].name);
      if (optarg)
        printf (" with arg %s", optarg);
      printf ("\n");
      break;
    case 'h':
      help (argv);
      exit (0);
    case 'v':
      verbose = atoi(optarg);
      break;
    case 'param':
      param = atoi(optarg);
      break;
    case '?':
      abort ();
    default:
      abort ();
    }
  }
}

int main (int argc, char ** argv)
{
  int verbose = 0;
  int param = 0;
  parse_args (argc, argv, verbose, param);
  cout << "verbose=" << verbose << " param=" << param << endl;
  return EXIT_SUCCESS;
}

I compile it with this command (gcc version 4.1.2 20080704 Red Hat 4.1.2-46):

g++ -Wall test.cpp

It tells me this:

test.cpp:44:10: warning: character constant too long for its type

And here is the result:

$ ./a.out -v 2 --param 3
c=118
c=0
option param with arg 3
c=-1
verbose=2 param=0

I tried to make it work on ideone but it doesn’t even recognize the option -v.

As indicated by trojanfoe in his comments of another question, it should be possible to use “long-only” options because GNU tar does it. However, GNU tar uses argp and I have difficulty understanding its source code.

Could someone give me a minimal example that works, with GNU getopt_long() or argp()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:41:42+00:00Added an answer on June 2, 2026 at 1:41 pm

    There are two problems:

    1. According to the example code (your link), the final option defined in the struct must be {0,0,0,0}. I recommend changing the definition to

      static struct option long_options[] =
        {
          {"help", no_argument, 0, 'h'},
          {"verbose", required_argument, 0, 'v'},
          {"param", required_argument, 0, 0},
          {0,0,0,0}
        };
      
    2. (And more importantly,) you must include code that actually processes the “param” option. You do that in the '0' case:

      case 0:
        if (long_options[option_index].flag != 0)
          break;
        if (strcmp(long_options[option_index].name,"param") == 0)
          param = atoi(optarg);
        break;
      

    As you can see I use the strcmp function to compare the strings; for that you need to #include <cstring>. By the way, you also need #include <cstdio> for your use of printf.

    With these changes in place, the program worked correctly for me (tested on GCC 4.5.1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which takes a long time to complete. I would like
I have a program which I would like to run every X min the
I have a program that reads data from a file line-by-line. I would like
I have a program that generates/'rolls' two dice. I would like to output these
I have a client/server program in TCP written in C, and I would like
I have an NSArray of Foos in an Objective-C program. I would like to
I have a problem with Java. I would like to write a program where
I have a table tv_programs (prog_id, channel_id, prog_name, prog_start_date, prog_end_date) I would like to
I'm writing a blackjack program and would like to be able to write the
I'm trying to link 10 computers together, the program I would like to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.