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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:24:43+00:00 2026-06-09T00:24:43+00:00

I have a program that needs to be called using: program parameter1 parameter2 -x1

  • 0

I have a program that needs to be called using:

program parameter1 parameter2 -x1 -y

but I feel that it should work if I do:

program -x1 -y parameter1 parameter2

or combinations thereof. How do I get parameter1 and parameter2 without a horrid dirty hack? At the moment I have a

while ((c = getopt (argc, argv, "x:y")) != -1){
   /* do stuff */
}

loop for the optional arguments, but what about the others? It would seem wrong to just look at argv[1] and argv[2] because they could be anywhere.

I’m sure there is an established solution to this.

  • 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-09T00:24:45+00:00Added an answer on June 9, 2026 at 12:24 am

    After you exit the while loop, optind points to the first non-option argument. Take a look at the getopt(3) man page:

       If  there  are  no  more option characters, getopt() returns -1.  Then optind is the
       index in argv of the first argv-element that is not an option.
    

    So your first non-option argument is argv[optind] and so forth.

    while ((c = getopt (argc, argv, "x:y")) != -1){
       /* do stuff */
    } 
    
    param1 = argv[optind]
    param2 = argv[optind+1]
    

    getopt will permute the arguments so that this will still be true even for your first example, where the option arguments are on the command line after the non-option arguments.

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

Sidebar

Related Questions

I have a program that needs to... In Activity A , do some jobs
I have a program that needs several third-party libraries, and at the moment it
I have a program that needs a lot of memory and want to set
I have a Flash program that needs to make url requests to send data
In an embedded program I have a screen object that needs to manage a
I have a test program called ftest. It loads .so files that contain tests
I have a program that runs in Python 2 and Python 3, but there
I have a fairly old unmanaged C++ program that called managed c# code (vs2003)
I'm trying to do the safe thing, and have a program that needs to
I have a oracle stored proc that needs to be called from my Java

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.