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 8722177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:21:03+00:00 2026-06-13T07:21:03+00:00

I currently have code that looks like this: while (( flags = getopt(argc, argv,

  • 0

I currently have code that looks like this:

while (( flags = getopt(argc, argv, "abc")) != -1){
    switch(flags){
    case 'a':
        dflag = 1;
        break;
    case 'b':
        rflag = 1;
        break;
    case 'c':
        cflag = 1;
        break;
    }
}

The problem is I wanted to include something like testprogram -c -d 1

Where c is required to handle the -d, without -c, testprogram -d 1 will just run without arguments.

I tried a large variety of things and I just can’t seem to get this to work.

Any help would be appreciated.

  • 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-13T07:21:04+00:00Added an answer on June 13, 2026 at 7:21 am

    I think there are two options available to you, one of which will work and one of which may or may not work.

    The ‘may or may not work’ option would be exemplified by:

    char *opts = "abc";
    char *opts_d = "abcd:";
    
    while ((opt = getopt(argc, argv, opts)) != -1)
    {
        switch (opt)
        {
        case 'a':
            aflag = 1;
            break;
        case 'b':
            bflag = 1;
            break;
        case 'c':
            cflag = 1;
            opts = opts_d;
            break;
        case 'd':
            dflag = 1;
            dvalue = optarg;
            break;
        default:
            ...error handling...
            break;
        }
    }
    

    I’m not sure whether there’s anything that prohibits you from changing the list of valid options on successive calls to [getopt()][1], but I do know it is not normally done to change the options as you go. So, treat with caution. Note that if the -c option is never found, the opt value of d will never be returned by getopt(), so that code will not be executed otherwise. Note that if the user enters a -d option before the -c, getopt() will report an invalid option when it processes the -d. You can control the error reporting; you would probably have to. (The optopt variable contains the actual option letter that was encountered as invalid.)

    The ‘will work’ option is:

    while ((opt = getopt(argc, argv, "abcd:")) != -1)
    {
        switch (opt)
        {
        case 'a':
            aflag = 1;
            break;
        case 'b':
            bflag = 1;
            break;
        case 'c':
            cflag = 1;
            break;
        case 'd':
            if (cflag == 0)
                ...report error about must use -c option before -d option...
            dflag = 1;
            dvalue = optarg;
            break;
        default:
            ...error handling...
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Entity in Code First Entity framework that currently looks like this:
I am currently working on a project where I have code that looks like
I have an object model that looks like this (pseudo code): class Product {
I currently have a case statement in bash that looks like so: restart) if
I currently have some code that looks like: -(UIView)someMethod { CGRectMake(0,0,100,100); UILabel *label =
I currently have this useful code that I found elsewhere on StackOverflow: form.DrawToBitmap(bmp, new
I have a dataset that looks like this: 0 _ _ 23.0186E-03 10 _
I have written my own 2-3-4 tree in java. Currently, my code looks like
I have a basic socket server that looks like this: IPEndPoint localEndPoint = new
I currently have code that does the following: private final static ExecutorService pool =

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.