{"id":1324,"date":"2015-04-25T00:01:17","date_gmt":"2015-04-25T07:01:17","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1324"},"modified":"2015-05-02T07:52:49","modified_gmt":"2015-05-02T14:52:49","slug":"parsing-the-command-line-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1324","title":{"rendered":"Parsing the Command Line II"},"content":{"rendered":"<p>When processing the <em>main()<\/em> function&#8217;s arguments, you must keep in mind the possibilities. Options are, after all, options. They might be there, they might not.<br \/>\n<!--more--><br \/>\nIn <a href=\" http:\/\/c-for-dummies.com\/blog\/?p=1315\">last week&#8217;s Lesson<\/a>, I presented code that required one specific option. That&#8217;s highly unusual. Instead, the option would be, well, optional. The following code is a more realistic representation of how that single argument would be processed:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    if(argc &gt;= 2)\r\n    {\r\n        if( strcmp(argv[1],\"hello\")==0 )\r\n        {\r\n            puts(\"Valid argument specified\");\r\n            return(0);\r\n        }\r\n        else\r\n        {\r\n            fprintf(stderr,\"Invalid argument specified\\n\");\r\n            return(1);\r\n        }\r\n    }\r\n    puts(\"No arguments specified, which is cool\");\r\n    return(0);\r\n}<\/pre>\n<p>When a single argument is present, the <code>argc<\/code> value is always 2 &mdash;. That test is made at Line 6 above. If the test is false, then no arguments are present and the entire command line parsing code chunk is skipped.<\/p>\n<p>The single valid option is the word <code>hello<\/code>, which is tested for at Line 8. When that specific option isn&#8217;t available, an error message is sent to <em>stderr<\/em> and the program terminates.<\/p>\n<p>Any additional arguments aren&#8217;t required, so they&#8217;re ignored. The code could test for that condition, and either quit or simply display an error message such as, &#8220;Superfluous arguments ignored.&#8221;<\/p>\n<p>In the following code, two arguments are required. They can be specified in any order, but both options must be present.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    int x,first,second;\r\n\r\n    if(argc != 3)\r\n    {\r\n        fprintf(stderr,\"Both arguments must be specified\\n\");\r\n        return(1);\r\n    }\r\n\r\n    for(x=1;x&lt;3;x++)\r\n    {\r\n        if(strcmp(argv[x],\"one\")==0)\r\n            first = 1;\r\n        if(strcmp(argv[x],\"two\")==0)\r\n            second = 1;\r\n    }\r\n\r\n    if( first && second )\r\n        puts(\"Both arguments specified. Thank you!\");\r\n    else\r\n    {\r\n        if(!first)\r\n            fprintf(stderr,\"Argument 'one' is missing\\n\");\r\n        if(!second)\r\n            fprintf(stderr,\"Argument 'two' is missing\\n\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code is pretty strict: If both arguments aren&#8217;t detected at Line 8, an error message is displayed and the program stops.<\/p>\n<p>At Line 14 the code cycles between available command line arguments. Each one is compared with the valid choice of either <code>one<\/code> or <code>two<\/code>. When a match is found, variables <code>first<\/code> or <code>second<\/code> are set accordingly. That way the program keeps track of the arguments internally.<\/p>\n<p>Finally, the <em>if-else<\/em> test starting at Line 22 confirms that the options were set. If not, error messages are output. The code doesn&#8217;t stop at that point, although it could.<\/p>\n<p>This code could easily be modified to provide for additional arguments. The key is to loop through all available arguments and compare them with valid options. I&#8217;ll cover that type of command line parsing in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1333\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The program requires two arguments in any order. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1324\">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-1324","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\/1324","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=1324"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1324\/revisions"}],"predecessor-version":[{"id":1357,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1324\/revisions\/1357"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}