LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2024, 12:15 PM   #1
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Parsing getopt (c). How to skip argument?


Code:
// return struct based on args
REGCHECKS
arg_process(int argc,
            char ** args)
{
   // declare struct and initialize
   REGCHECKS t1;
   init_regchecks_struct(&t1,
                         false,
                         false);

   // load linux version into struct
   // fail if os-release file not found
   if (get_linux_version(&t1) != 0) {
      init_regchecks_struct(&t1,
                            true,
                            true);
      t1.bailout = true;

      return(t1);
   };

   int option = 0;
   int optcounter = 1;
   bool patternbypass = false;
   while ((option = getopt(argc,
                           args, 
                           "ap")) != -1) {
      if (t1.bailout) {
         break;
      }
      switch (option)
      {
         case 'a':
            if (args[optcounter + 1]) {
               t1.architecture = strdup(args[optcounter + 1]);
               optcounter += 2;
            } else {
               init_regchecks_struct(&t1,
                                     true,
                                     true);
            }
            break;
         case 'p':
            // the arg here may start with a -
            if (args[optcounter +1]) {
               test_formatting(args[optcounter + 1],
                               &t1);
               optcounter += 2;
            } else {
               init_regchecks_struct(&t1,
                                     true,
                                     true);
            }
            break;
         default:
            init_regchecks_struct(&t1,
                                  true,
                                  true);
            break;
      }
   }

   return(t1);
}
For the p option sometimes I may want my input to start with a -.
Code:
-p -doc
or some such. is there a way to skip past the next option and ignore it. I tried setting optind + 1 and it works but it ignores any other args. The ultimate goal is to have no specific required order for arguments.
 
Old 05-14-2024, 12:50 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,777

Rep: Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935
Code:
getopt(argc,args,"ap")
By definition any character followed by a colon (:) requires an argument, two colons means the argument is optional. Since your p is not followed by any colons -doc is not considered a valid argument or even option. I don't think it matters if the argument starts with a -.

It should not matter in what order the -a or -p are entered on the command line. I would guess if you are not following getopt syntax then you need to manually parse your command line arguments.

Last edited by michaelk; 05-14-2024 at 12:51 PM.
 
1 members found this post helpful.
Old 05-14-2024, 12:57 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,021

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
you need to decide if -p requires an additional parameter, and how do you want to pass it. Usually it should come immediately after the flag, so -a -p -doc is ok, -ap -doc is ok, but -doc -a -p or -p -a -doc or -a -doc -p are not.
Anyway, you need to specify exactly what is expected and allowed and if your requirements are incompatible with getop you need to implement your own parser. Like rsync or ssh.
 
1 members found this post helpful.
Old 05-14-2024, 12:58 PM   #4
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Original Poster
Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
That did it. Thank you. i'll need to go over that man page again. missed the part on the colons.

The call would be something like this
Code:
aptsort -a amd64 -p pattern
For now those are the only 2 options I've got on this program. The colons though did it. Works a treat now.

Last edited by jmgibson1981; 05-14-2024 at 12:59 PM.
 
Old 05-14-2024, 01:33 PM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,021

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by jmgibson1981 View Post
That did it. Thank you. The colons though did it. Works a treat now.
In that case you might want to mark the thread as solved.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python: argument parsing PoleStar Programming 3 12-30-2015 08:49 PM
[SOLVED] Parsing mountpoint from /etc/mtab (parsing "string") in C deadeyes Programming 3 09-06-2011 05:33 PM
message sending failed : Error[22 ] invalid argument .....but each and every argument rakeshranjanjha Linux - Software 2 01-07-2008 11:22 PM
shell script: can getopt process more than one argument per option? BrianK Programming 2 12-13-2007 05:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration