Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse command-line arguments in C++?

How to effectively parse this command line in C++?

program -parameter1=value1 -parameter2=value2 -parameter3=value3

How to effectively drop a combination of the parameter and value

-parameter=value

I am trying to use this code but it does not work properly:

parameter[256], value[256],
while ( --argc > 0 )
{
   if ( *argv[argc] == '-' )
   {
       for ( char * text = argv[argc]; ; )
       {    
            switch ( * ( ++ text ) )
            {
                case '=' :
                {
                   *value = *(  text );
                    break;
                }

                default:
                {
                   *parameter = *text;
                }
            }
         }

       //Testing parameters and values
    }
}

Thanks for your comments and improvements.

like image 443
abcde Avatar asked Jun 19 '26 10:06

abcde


1 Answers

Did you consider boost::program_options or if you can't use boost, getopt_long?

like image 55
Mark B Avatar answered Jun 20 '26 23:06

Mark B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!