{"id":1315,"date":"2015-04-18T00:01:54","date_gmt":"2015-04-18T07:01:54","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1315"},"modified":"2015-04-25T07:27:38","modified_gmt":"2015-04-25T14:27:38","slug":"parsing-the-command-line-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1315","title":{"rendered":"Parsing the Command Line I"},"content":{"rendered":"<p>About a year ago, I wrote a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=630\">post<\/a> on reading the command line arguments. It&#8217;s a process that takes place all the time, not only when running programs in a terminal window but also for graphical operating systems. Knowing how to manipulate command line arguments is important.<br \/>\n<!--more--><br \/>\nOne thing I didn&#8217;t mention in that earlier post is that the two arguments for the <em>main()<\/em> function don&#8217;t always have to be named <code>argc<\/code> and <code>**argv<\/code>. Those are merely the traditional names, probably first used by Brian Kernighan and Dennis Ritchie when they wrote their seminal book, <em>The C Programming Language<\/em>.<\/p>\n<p>If you like, you can name <em>main()<\/em> function&#8217;s arguments anything, as long as the first is an integer value and the second is a pointer array. To wit:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(int c, char **s)\r\n{\r\n    printf(\"This program is named %s\\n\",*s);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code compiles and it runs and it spews out the program name, argument zero on the command line.<\/p>\n<p>A more devilish trick is being able to read command line arguments and determine whether or not they&#8217;re valid. This type of processing, officially known as <em>parsing the command line<\/em>, can occupy a great amount of the code. It gets really hairy, especially when dozens of options are available. Worse: The user can type them in any order, lump them together, forget some, or do all kinds of mischief. Truly, parsing a command line can be an art form.<\/p>\n<p>In the following code, one argument must be specified, <em>hello<\/em>. It must match that case exactly. The code checks for the argument and further confirms that it&#8217;s the proper one.<\/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 != 2)\r\n    {\r\n        fprintf(stderr,\"Please specify a valid argument\\n\");\r\n        return(1);\r\n    }\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}<\/pre>\n<p>The first argument on any command line is always the program name. So when another argument is specified, the argument count (<code>argc<\/code> above) is greater than one. Specific to this code, the second argument is required, so <code>argc<\/code> must equal 2 or the program is unable to run.<\/p>\n<p>The single command line argument dwells as a string referenced by variable <code>argv[1]<\/code>. Remember that <code>argv[0]<\/code> is the program&#8217;s name. One thing that throws even experienced programmers is keeping in mind that argument 2 is accessed via element one in the <code>argv[]<\/code> array.<\/p>\n<p>Above, the <em>strcmp()<\/em> function checks for the proper argument. If specified, the program is pleased, otherwise an error message is output. And the error output goes to the <em>stderr<\/em> device, which is typically the display. You don&#8217;t want to use a <em>puts()<\/em>  or <em>printf()<\/em> function for an error message, which can be redirected and potentially overlooked.<\/p>\n<p>Of course, there&#8217;s no logic for a program to require a single, specific argument; arguments are options that affect the program&#8217;s processing or output. In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1324\">next week&#8217;s Lesson<\/a> I&#8217;ll cover how to parse multiple options.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Knowing your <code>argc<\/code> and <code>argv<\/code>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1315\">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-1315","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\/1315","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=1315"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":1345,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1315\/revisions\/1345"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}