Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #42926 > unrolled thread

Command Line Option Validation

Started bywillmann817@gmail.com
First post2014-04-14 17:08 -0700
Last post2014-04-15 00:58 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c


Contents

  Command Line Option Validation willmann817@gmail.com - 2014-04-14 17:08 -0700
    Re: Command Line Option Validation Ian Collins <ian-news@hotmail.com> - 2014-04-15 12:36 +1200
    Re: Command Line Option Validation Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-15 00:58 -0700

#42926 — Command Line Option Validation

Fromwillmann817@gmail.com
Date2014-04-14 17:08 -0700
SubjectCommand Line Option Validation
Message-ID<e4fc3d3a-5f39-4ecb-ba66-85e188083e19@googlegroups.com>
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

        }
    }


}

[toc] | [next] | [standalone]


#42928

FromIan Collins <ian-news@hotmail.com>
Date2014-04-15 12:36 +1200
Message-ID<br3d7hF3g7rU4@mid.individual.net>
In reply to#42926
willmann817@gmail.com wrote:
> 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.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#42953

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2014-04-15 00:58 -0700
Message-ID<95891f95-3d0e-4c95-a6d6-8e27fcd5a254@googlegroups.com>
In reply to#42926
On Tuesday, April 15, 2014 1:08:11 AM UTC+1, William Koch wrote:
> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web