1

How to verify if the user has passed only integer value to a getopt option from the command line ? User should pass only positive integer values .

Isdigit() function does not work properly here .

                case 's' :
                    flags=1;

                    start = atoi(optarg);

How to check if start contains only integer value ?

                ./prog -scf

By default its taking s value as 0 when any character is entered , since i am using atoi function but here actually it should give error as cf is passed .

Even tried strtol() , but no use

            start = (int) strtol(optarg, &ptr, 10);

even if i use strtol() function , it works only if both number and strings are passed .

            eg ./prog -s5abc        --> this works

            eg  ./prog -sabc         -->does not work

here again start takes 0 value , as only charters is passed ! but if user itself passes 0 then how can i handle error ?

           eg ./prog -s0        --> s takes 0 value, valid

           eg  ./prog -sabc     --> s takes 0 value , but invalid
programmer
  • 71
  • 1
  • 12
  • `if (start < 0) { ... };` ? – David Ranieri May 09 '17 at 06:47
  • What do you mean by "*does not work properly*"? – cdarke May 09 '17 at 06:48
  • See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). – David C. Rankin May 09 '17 at 06:52
  • Possible duplicate of [atoi — how to identify the difference between zero and error?](http://stackoverflow.com/questions/8871711/atoi-how-to-identify-the-difference-between-zero-and-error) – kaylum May 09 '17 at 06:54
  • Try looking into `optarg` to see if it only contains numeric characters? – Silveris May 09 '17 at 07:16
  • @Silveris thats my question how do i do that ? isdigit() , atoi , strtol , none of this functions gives proper error if only characters have been passed . – programmer May 09 '17 at 07:25
  • What does `isdigit` return when you pass a non-digit character? In which way does this not work? – Gerhardh May 09 '17 at 07:43
  • @Gerhardh isdigit returns o for both char or digit here , since am not using unsigned values . It works only for unsigned type ! – programmer May 09 '17 at 07:59
  • And you cannot simply check for one single `'-'` at first position and use `isdigit` for following characters? – Gerhardh May 09 '17 at 10:51
  • BTW: I doubt, that `isdigit` returns `0` for characters that are digits. – Gerhardh May 09 '17 at 10:52
  • @Silveris The code works perfect ! In the for loop , optarg[i] != 0 Can you explain this condition please ? – programmer May 09 '17 at 12:35
  • @Silveris Is there any simple solution for this question ? http://stackoverflow.com/questions/43846090/getopt-error-handling-for-multiple-comma-seprated-values/43846698#43846698 – programmer May 09 '17 at 12:37
  • A string always ends with the character `'\0'` and its value in ASCII is `0`. For example the string `abc` is in fact `abc\0`, so you have to loop until you reached the end of the string: `\0`. – Silveris May 09 '17 at 12:38
  • @Silveris exactly but for string "009" , How is it working ? , First only it encounters 0 right . – programmer May 09 '17 at 12:49
  • @programmer If it is a string it will be `"009\0"` too. Because in C strings are arrays of `char`, and '0' is not the same than 0, '0' is the ASCII value of 0 (the value stocked in a char to write 0) which in fact is 48. If you try this: `printf("%d - %d\n, '0', 0);` it will display `48 - 0`. – Silveris May 09 '17 at 12:56
  • @programmer I answered your other question two, hope it helped :) – Silveris May 09 '17 at 13:17
  • @Silveris Can you explain this line of code : token = strtok(NULL,","); without this line loop goes infinitely , but why ? – programmer May 10 '17 at 05:49
  • @Silveris thank you so much both the codes are working fine , but for the other answer : ./prog -a knn,knn,knn this should give error but it wont , only once the value can be given , for this what can we do ? – programmer May 10 '17 at 05:52
  • @programmer `token = strtok(NULL, ",");` prevents from infinite loops because passing `NULL` to `strtok()` means you want the next token on the string you passed first. In this case `optarg`. I also edited my answer to show you how to get an error in the case you showed me. – Silveris May 11 '17 at 07:34
  • @Silveris now am getting error , am going need this value as it is , -a knn ,svm,lr but after error checking it contains null value . – programmer May 11 '17 at 08:42
  • @Silveris Check out the other post i have mentioned my output , am still trying your previous code , it was working fine but when i implemented it completely its giving error – programmer May 11 '17 at 09:01

1 Answers1

0

As you are working with signed numbers, this should do:

if (optarg[0] != '-' && (optarg[0] < '0' || optarg[0] > '9'))
    *error handling*
for (size_t i = 1; optarg[i] != 0; i++)
    if (optarg[i] < '0' || optarg[i] > '9')
        *error handling*
Silveris
  • 1,048
  • 13
  • 31