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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:37:52+00:00 2026-06-08T03:37:52+00:00

I know this topic has probably been done to death, but I’ve been unable

  • 0

I know this topic has probably been done to death, but I’ve been unable to find anything that made me understand it. I need to enter a value, for instance an IP address, into the command line and pass it to a function.

Below is my getopt_long function.

while (1)
{
    static struct option long_options[] =
    {
        /* Options */
    {"send",       no_argument,       0, 's'}, /* args s and r have no function yet */
    {"recieve",    no_argument,       0, 'r'},
    {"file",       required_argument, 0, 'f'}, 
    {"destip",     required_argument, 0, 'i'},
    {"destport",   required_argument, 0, 'p'},
    {"sourceip",   required_argument, 0, 'o'},
    {"sourceport", required_argument, 0, 't'},
    {0, 0, 0, 0}
    };

   int option_index = 0;

   c = getopt_long (argc, argv, "srf:d:i:p:o:t:",
                long_options, &option_index);

              /* Detect the end of the options. */
   if (c == -1)
     break;

   switch (c)
     {
     case 0:
       /* If this option set a flag, do nothing else now. */
       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 's':
       puts ("option -s\n");
       break;

     case 'r':
       puts ("option -r\n");
       break;

     case 'f':
       printf ("option -f with value `%s'\n", optarg);
       break;

     case 'i':
       printf ("option -i with value `%s'\n", optarg);
       break;

     case 'p':
       printf ("option -p with value `%s'\n", optarg);
       break;

     case 'o': 
       printf ("option -o with value `%s'\n", optarg);
       break;

     case 't': 
       printf ("option -t with value `%s'\n", optarg);
       break;

     case '?':
       /* Error message printed */
       break;

     default:
       abort ();
     }
}

/* Print any remaining command line arguments (not options). */
if (optind < argc)
{
    printf ("non-option ARGV-elements: ");
    while (optind < argc)
    printf ("%s ", argv[optind++]);
    putchar ('\n');
}

This is where I need the value to go (part of a pretty standard tcp struct)

ip->iph_sourceip = inet_addr(arg);

How do I do this correctly? I researched quite a bit, and although many cover similar topics they do not seem to explain my issue all too well.

  • 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-08T03:37:55+00:00Added an answer on June 8, 2026 at 3:37 am

    When using getopt, you’ll typically declare variables that match the various switches, so that you can act on them later, once argument parsing has completed; some arguments you can act on immediately during argument processing.

    For instance you might have an address variable for storing the address from the -i command, similarly for the -p argument:

    in_addr_t address;
    int port;
    
    // ... later in your switch statement:
    switch (c)
    {
        // ...
    
       case 'i':
           printf("option -i with value `%s'\n", optarg);
           address = inet_addr(optarg);
           break;
       case 'p':
           printf("option -p with value `%s'\n", optarg);
           // be sure to add handling of bad (non-number) input here
           port = atoi(optarg);
           break;
        // ...
    }
    
    // later in your code, e.g. after arg parsing, something like:
    send_tcp(address, port);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this topic has been beaten to death, but it seems that
I know this topic has been discussed to death, but there is one thing
I know this topic has been beaten to death a little on SO, but
I know this topic has been beat to death but a lot of the
I know that this question has been done to death at StackOverflow and that
ok, I know that this topic has been addressed several times on here, but
I know that this topic has been already beaten enough, but I still don't
I know this topic has been discussed but I think it has some differences.
I know this is a recurrent/classical topic but I did not found anything that
I know this topic has been asked, but the posts are all out of

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.