How do I get the option -10 from command line arguments- “tail -10“. getopt function finds ‘1’ character. But how do I access the string “10”?
If this can be done by getopt_long, an example would help. Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unless you intend for
-1to be an option with0as its argument, the answer is you don’t.getoptis only made for processing options that fit the standard POSIX utilities’ option syntax. It may be possible to use GNUgetopt_longfor this purpose, or you could just write your ownargvparser (it’s easy).Edit: Actually I think I misread what you want. If you want
-followed by any number to be interpreted as an option with that numeric value, I don’t think there’s any version ofgetoptthat will work. There’s no way you can special-case every single number as an option, and if you simply tellgetoptthat all of the digits are option characters that take arguments,-123will be interpreted as a-1option with an argument of23(which is fine, you can interpret it from there), but a lone-1will cause the nextargvelement to geteatenas an argument to-1, which is difficult or impossible to recover from.