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

  • SEARCH
  • Home
  • 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 6975035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:18:57+00:00 2026-05-27T17:18:57+00:00

Is there an alternative way to pass two arguments to an option as a

  • 0

Is there an alternative way to pass two arguments to an option as a single string when using getopt()? Normally I would do the following:

./command -o "key value" [command arguments]

Then I would have to split the argument string explicitly:

 while ((op = getopt(argc, argv, "o:")) != EOF) {
    switch (op) {
             case 'o': 
                 char* result = NULL;
                 result = strtok_r(optarg," ");
                 while(result) {
                     /* DO STUFF */
                     result = strtok(NULL," ");
                 }

                 break;
             default:
                 printUsage()
                 break;
   }

So, I am wondering if it is possible to use the following and make getopt() treat "value" as -o second argument rather than the command argument?

./command -o key value [command arguments]
  • 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-05-27T17:18:58+00:00Added an answer on May 27, 2026 at 5:18 pm

    The POSIX standard getopt() does not support multiple arguments for a single flag letter. There are a couple of options available to you.

    One option is to specify the names as arguments to separate option letters (or use the same one twice):

    ./command -o key -v value
    ./command -o key -o value
    

    Another option is to use the POSIX standard getsubopt() and a command line notation such as:

    ./command -o key=value
    

    This works pretty well for your shown scenario if the set of keys is relatively small (not so well if they are large, but you could do without getsubopt() then and simply parse optarg for the = yourself.

    Another option is to write a command line parser that will accept multiple arguments as you require. I wrote one to be able to parse the diverse and ill-disciplined argument handling on programs that we use at work – and it is not dreadfully difficult (though it wasn’t altogether easy, either). It specifically has to handle N (N > 1) option values after a single flag. Contact me (see my profile) if you want to look at the source. There are some tricky issues to resolve, such as what happens if you expect 3 names after the option letter, but one of the first three arguments starts with a dash -, or -- appears. I only allow a fixed number of arguments, not a variable number, but that is in part because my use cases didn’t need to cover a variable number (and it doesn’t sound as if yours would, either).

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

Sidebar

Related Questions

Is there any alternative to this: Organizations.Include(Assets).Where(o => o.Id == id).Single() I would like
I want two objects to share a single string object. How do I pass
Is there any alternative to Axis2? Or the way to make it work (different
Is there any alternative image manipulation library for .net? I would prefer something that
Is there an alternative to history.go(-1) for FireFox and Safari. Any Help would be
Is there a way to pass a wrap and unwrap a TObject descendent in
There are basically two ways to pass arrays to a subroutine in Fortran 90/95:
Is there any alternate way to create stored procedure without putting all query in
is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks
Is there any alternative for WPF (windows presentation foundation) in python? http://msdn.microsoft.com/en-us/library/aa970268.aspx#Programming_with_WPF

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.