{"id":4829,"date":"2021-07-03T00:01:04","date_gmt":"2021-07-03T07:01:04","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4829"},"modified":"2021-07-10T08:08:00","modified_gmt":"2021-07-10T15:08:00","slug":"discovering-command-line-options-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4829","title":{"rendered":"Discovering Command Line Options, Part II"},"content":{"rendered":"<p>I suppose smart equates to quirky in most programming circumstances. This maxim definitely holds true for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4823\">the <em>getopt()<\/em> function<\/a>. Before you can appreciate this function and put it to use, you must understand how it works and why it can be quirky.<br \/>\n<!--more--><br \/>\nHere is a sample <em>getopt()<\/em> statement:<\/p>\n<p><code>r = getopt(argc,argv,\"ABC\");<\/code><\/p>\n<p>The <code>argc<\/code> and <code>argv<\/code> arguments are stolen directly from the <em>main()<\/em> function. The third argument implies that this program can swallow up to three optional switches: <code>-A<\/code>, <code>-B<\/code>, and <code>-C<\/code>. These must be specified with the leading dash and the letters are case sensitive. Here is the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_07_03-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_07_03-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    int r;\r\n\r\n    r = getopt(argc,argv,\"ABC\");\r\n    printf(\"The getopt() function returned %d\\n\",r);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>getopt()<\/em> function at Line 8 reads the command line arguments. The return value from the function, stored in variable <code>r<\/code>, is output as an integer at Line 9. The purpose of this program is to discover how <em>getopt()<\/em> processes input. Sample runs are in order, first with no arguments:<\/p>\n<p><code>$ .\/a.out<br \/>\nThe getopt() function returned -1<\/code><\/p>\n<p>The return value of -1 means that all command line arguments have been parsed. It would be up to the code at this point to determine whether this condition is wanted or valid. Remember, the return value of -1 isn&#8217;t really an error, just a value open to interpretation.<\/p>\n<p>Next, the valid switch <code>-A<\/code> is specified:<\/p>\n<p><code>$ .\/a.out -A<br \/>\nThe getopt() function returned 65<\/code><\/p>\n<p>The function returned the ASCII code value for <code>'A'<\/code>, 65. Note that the switch is specified as <code>-A<\/code>. Here&#8217;s what you see when just <code>A<\/code> is specified:<\/p>\n<p><code>$ .\/a.out A<br \/>\nThe getopt() function returned -1<\/code><\/p>\n<p>The function returns -1 for an unrecognized argument. Because the <code>A<\/code> isn&#8217;t prefixed with a dash, the function assumes it&#8217;s something other than a switch; the <em>getopt()<\/em> function ignores command line arguments not prefixed with a dash.<\/p>\n<p>Here is the function&#8217;s reaction when you type a little <code>-a<\/code> as a switch:<\/p>\n<p><code>$ .\/a.out -a<br \/>\n.\/a.out: illegal option -- a<br \/>\nThe getopt() function returned 63<\/code><\/p>\n<p>The first line of output is generated by the <em>getopt()<\/em> function itself: <code>-a<\/code> is an &#8220;illegal&#8221; option. But the program continues, the <em>printf()<\/em> generating it output: The code presented is ASCII 63, the <code>'?'<\/code> character.<\/p>\n<p>The ugly error message is output by default, though you can suppress it. To do so, set the global variable <code>opterr<\/code> to zero. This variable is an <em>extern int<\/em> defined in the <code>unistd.h<\/code> header file. Here&#8217;s is the updated code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_07_03-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_07_03-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    int r;\r\n\r\n    opterr = 0;\r\n    r = getopt(argc,argv,\"ABC\");\r\n    printf(\"The getopt() function returned %d\\n\",r);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is the output with the <code>opterr<\/code> variable set to zero (Line 8 above):<\/p>\n<p><code>$ .\/a.out -a<br \/>\nThe getopt() function returned 63<\/code><\/p>\n<p>The takeaway here is that return code 63 indicates an invalid option. Your code can do whatever in such a circumstance.<\/p>\n<p>One last test! The final character in the third argument, <code>C<\/code>:<\/p>\n<p><code>$ .\/a.out -C<br \/>\nThe getopt() function returned 67<\/code><\/p>\n<p>ASCII code 67 represents a &#8216;C&#8217;.<\/p>\n<p>Each option is detected. Bogus options return value 63, the <code>'?'<\/code> character. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4856\">Next week&#8217;s Lesson<\/a> continues exploration of the <em>getopt()<\/em> function, with the method to review multiple options and how to determine what character was specified as an invalid option.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your code need not toil with processing command line switches as the C library has a function that deftly handles the job. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4829\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4829","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4829"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4829\/revisions"}],"predecessor-version":[{"id":4875,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4829\/revisions\/4875"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}