Command Line Option Validation

Discussion in 'C Programming' started by willmann817, Apr 15, 2014.

  1. willmann817

    willmann817 Guest

    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

    }
    }


    }
     
    willmann817, Apr 15, 2014
    #1
    1. Advertisements

  2. willmann817

    Ian Collins Guest

    Use strtol to parse the the appropriate argv value and check the result.
     
    Ian Collins, Apr 15, 2014
    #2
    1. Advertisements

  3. willmann817

    Stefan Ram Guest

    #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 ); }
     
    Stefan Ram, Apr 15, 2014
    #3
  4. willmann817

    Stefan Ram Guest


    !*p && p - num > 0 ? main4( ...
     
    Stefan Ram, Apr 15, 2014
    #4
  5. willmann817

    Stefan Ram Guest

    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 ); }
     
    Stefan Ram, Apr 15, 2014
    #5
  6. 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.
     
    Malcolm McLean, Apr 15, 2014
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.