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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:11:21+00:00 2026-06-15T17:11:21+00:00

I’m trying to parse two options in a C program. The program is called

  • 0

I’m trying to parse two options in a C program.

The program is called like this:

./a.out [OPTIONS] [DIRECTORY 1] [DIRECTORY 2]

The program syncs two directories and has two options. (-r) for recursive syncing (folder inside folder), and (-n) to copy file from local to remote in case it doesn’t exist in remote.

Options are:
-r : recursive
-n : copy file if it doesn't exist in remote folder

So calling:

./a.out -r D1 D2

would recursively sync all files and directories from D1 to D2. Files presents in D1 and not present in D2 are ignored.

And calling:

./a.cout -rn D1 D2

would do the same thing but files present in D1 and not present in D2 are copied to D2.

The problem is that calling ./a.out -rn is not the same as calling ./a.out -nr and ./a.out -r -n is not working neither because (-n) is not D1.

Here is how I implement the main.

int main(int argc, char** argv) {
  int next_option = 0;
  const char* const short_options = "hrn:";

  const struct option long_options[] = {
    { "help",      0, NULL,  'h' },
    { "recursive", 1, NULL,  'r' },
    { "new", 1, NULL,  'n' },
    { NULL,        0, NULL,  0   }
  };

  int recursive = 0;
  int new = 0;

  do {
    next_option = getopt_long(argc, argv, short_options, long_options, NULL);

    switch(next_option) {
      case 'r':
        recursive = 1;
        break;

      case 'n':
        new = 1;
        break;

      case 'h':
        print_help();
        return 0;

      case -1:
        break;

      default:
        print_help();
        return -1;
    }

  } while(next_option != -1);

  sync(argv[2], argv[3], recursive, new);

  return EXIT_SUCCESS;
}
  • 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-15T17:11:22+00:00Added an answer on June 15, 2026 at 5:11 pm

    You have two (potential) problems here:

    1. You have a stray : in your short option string. This makes -n take an option that swallows any following r. You also have the long options set to take mandatory arguments.

    2. You’ve hard coded the argument numbers in an inflexible way, and you don’t test that they exist.

    Try this:

    int main(int argc, char** argv) {
      int next_option = 0;
      const char* const short_options = "hrn";
      extern int optind;
    
      const struct option long_options[] = {
        { "help",      no_argument, NULL,  'h' },
        { "recursive", no_argument, NULL,  'r' },
        { "new",       no_argument, NULL,  'n' },
        { NULL,        no_argument, NULL,  0   }
      };
    
      int recursive = 0;
      int new = 0;
    
      do {
        next_option = getopt_long(argc, argv, short_options, long_options, NULL);
    
        switch(next_option) {
          case 'r':
            recursive = 1;
            break;
    
          case 'n':
            new = 1;
            break;
    
          case 'h':
            print_help();
            return 0;
    
          case -1:
            break;
    
          default:
            print_help();
            return -1;
        }
    
      } while(next_option != -1);
    
      if (optind + 1 >= argc)
        return -1;
    
      sync(argv[optind], argv[optind+1], recursive, new);
    
      return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is

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.