Command Line Option Validation

W

willmann817

when the user runs my program from the command line I require them to type

myProgram -k <some_value>

<some_value> needs to be a positive integer between 1 and 100. How do I assure they do not type in a string or something like that. Here is what I have so far but I am not sure what to do from here?

int main(int argc, char *argv []){
//Getting command line arguments using getopt.
while((ch = getopt(argc, argv, "k:m:s")) != EOF){
switch(ch){
case "k":
if

}
}


}
 
I

Ian Collins

when the user runs my program from the command line I require them to
type

myProgram -k <some_value>

<some_value> needs to be a positive integer between 1 and 100. How
do I assure they do not type in a string or something like that.
Here is what I have so far but I am not sure what to do from here?

int main(int argc, char *argv []){
//Getting command line arguments using getopt.
while((ch = getopt(argc, argv, "k:m:s")) != EOF){
switch(ch){
case "k":
if

Use strtol to parse the the appropriate argv value and check the result.
 
S

Stefan Ram

myProgram -k <some_value>
<some_value> needs to be a positive integer between 1 and 100.

#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS, atoi */
#include <string.h> /* strcmp */

int main4( char const * num )
{ int const i = atoi( num );
return i >= 1 && i <= 100 ? EXIT_SUCCESS : EXIT_FAILURE; }

int main3( char const * num )
{ char const * p;
for( p = num; *p >= '0' && *p <= '9' &&( p - num )< 4; ++p );
return !*p ? main4( num ): EXIT_FAILURE; }

int main2( char const * const argv[] )
{ return argv[ 0 ] && !strcmp( argv[ 0 ], "-k" ) ?
main3( argv[ 1 ]): EXIT_FAILURE; }

int main1( int const argc, char const * const argv[] )
{ int const kpos = argc - 2; return kpos >= 0 ?
main2( argv + kpos ): EXIT_FAILURE; }

int main()
{ char const * const argv[] ={ "myProgram", "-k", "27" };
return main1( sizeof argv / sizeof 0[ argv ], argv ); }
 
S

Stefan Ram

refactor: »static« and function names:

#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS, atoi */
#include <string.h> /* strcmp */

static int check_value( char const * num )
{ int const i = atoi( num );
return i >= 1 && i <= 100 ? EXIT_SUCCESS : EXIT_FAILURE; }

static int check_for_numeral( char const * num )
{ char const * p;
for( p = num; *p >= '0' && *p <= '9' &&( p - num )< 4; ++p );
return !*p && p - num > 0 ? check_value( num ): EXIT_FAILURE; }

static int check_for_hyphen_k( char const * const argv[] )
{ return argv[ 0 ] && !strcmp( argv[ 0 ], "-k" ) ?
check_for_numeral( argv[ 1 ]): EXIT_FAILURE; }

static int check_number_of_arguments
( int const argc, char const * const argv[] )
{ int const kpos = argc - 2; return kpos >= 0 ?
check_for_hyphen_k( argv + kpos ): EXIT_FAILURE; }

int main()
{ char const * const argv[] ={ "myProgram", "-k", "27" };
return check_number_of_arguments( sizeof argv / sizeof 0[ argv ], argv ); }
 
M

Malcolm McLean

when the user runs my program from the command line I require them to type

myProgram -k <some_value>

<some_value> needs to be a positive integer between 1 and 100. How do
I assure they do not type in a string or something like that. Here is
what I have so far but I am not sure what to do from here?

int main(int argc, char *argv []){

//Getting command line arguments using getopt.
while((ch = getopt(argc, argv, "k:m:s")) != EOF){
switch(ch){
case "k":
if

}

}

}

Go onto my website
http://www.malcolmmclean.site11.com/www/

and download the options parser. It does that type of validation for you.
The code's easy to read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top