{"id":187,"date":"2008-08-05T09:57:16","date_gmt":"2008-08-05T17:57:16","guid":{"rendered":"http:\/\/wp.colliertech.org\/cj\/?p=187"},"modified":"2008-08-08T12:03:32","modified_gmt":"2008-08-08T20:03:32","slug":"writing-free-software-part-3-command-line-options","status":"publish","type":"post","link":"https:\/\/wp.c9h.org\/cj\/?p=187","title":{"rendered":"Writing Free Software &#8211; Part 3: Command line options"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>In this installment, we will look at parsing command-line options.  We&#8217;re going to use <a href=\"http:\/\/www.jprl.com\/Blog\/index.html\">Jon Pryor<\/a>&#8216;s <a href=\"http:\/\/www.ndesk.org\/Options\">NDesk.Options<\/a> library.  Since he has not made a .deb package yet, we&#8217;ll use curl to integrate NDesk.Options.cs into our codebase.<\/p>\n<h2>Get back to our workspace<\/h2>\n<p><code><b>$<\/b> cd ~\/src\/greeting<\/code><\/p>\n<h2>Get curl<\/h2>\n<p><code><b>$<\/b> sudo apt-get install curl<\/code><\/p>\n<h2>Pull down a copy of NDesk.Options<\/h2>\n<p><code><b>$<\/b> curl &quot;http:\/\/git.ndesk.org\/?p=ndesk-options;a=blob_plain;f=src\/NDesk.Options\/NDesk.Options\/Options.cs;hb=c52aa20569b49b620deb0fd28b19909b7c577d47&quot; &gt; NDesk.Options.cs<\/code><\/p>\n<h2>Update our Makefile<\/h2>\n<p>Updates are bolded below.<\/p>\n<pre style=\"color: grey; margin: 0px; padding: 0px;\"><b>$<\/b> cat &gt; Makefile\r\n# the first target is the one executed when no target is named\r\nall: Greeting.exe\r\n#don't do anything\r\n\r\n# running 'make clean' will execute this target\r\nclean:\r\n        rm Greeting.exe\r\n\r\n# running 'make Greeting.exe' will execute this target\r\nGreeting.exe: Greeting.cs <b>NDesk.Options.cs<\/b>\r\n# the same as gmcs -out:Greeting.exe Greeting.cs <b>NDesk.Options.cs<\/b>\r\n        gmcs -out:$@ $^\r\n<\/pre>\n<h2>Update our C# code<\/h2>\n<p>Updates are bolded below.<\/p>\n<pre style=\"color: grey; padding: 0px; margin: 0px;\"><b>$<\/b> cat &gt; Greeting.cs\r\nusing System;        \/\/ This brings in Console.Writeline, used below\r\n<b>using NDesk.Options; \/\/ This brings in the options parser used below<\/b>\r\n\r\nclass Greeting {\r\n  <b>private static string [] planet =\r\n    { \"World\", \"Mercury\", \"Venus\", \"Earth\", \"Mars\", \"Jupiter\", \"Saturn\",\r\n      \"Uranus\", \"Neptune\", \"Pluto\" };<\/b>\r\n\r\n  \/\/ This is the program entry point\r\n  public static int Main( string [] argv ){\r\n\r\n    <b>\/\/ default to \"World\"\r\n    int planetNum = 0;\r\n\r\n    \/\/ set up an option parser\r\n    var p = new OptionSet () {\r\n        { \"n|number=\",  \"The planet number to greet\",\r\n          (int v) => planetNum = (v < planet.Length ? v : 0) }\r\n    };\r\n    p.Parse (argv);<\/b>\r\n\r\n    \/\/ A simple operation to confirm that our app is running:\r\n    Console.WriteLine(\"Greetings, <b>{0}<\/b>!\"<b>, planet[planetNum]<\/b>); \r\n\r\n    \/\/ Well-behaved software returns an int from Main\r\n    return 0;\r\n  }\r\n}\r\n<\/pre>\n<h2>Compile<\/h2>\n<p><code><b>$<\/b> make<\/code><\/p>\n<h2>Run it a few times<\/h2>\n<p><b>$<\/b> .\/Greeting.exe<br \/>\nGreetings, World!<br \/>\n<b>$<\/b> .\/Greeting.exe  &ndash;n 1<br \/>\nGreetings, Mercury!<br \/>\n<b>$<\/b> .\/Greeting.exe  &ndash;&ndash;number 2<br \/>\nGreetings, Venus!<br \/>\n<b>$<\/b> .\/Greeting.exe  &ndash;&ndash;number 10<br \/>\nGreetings, World!<br \/>\n<b>$<\/b> .\/Greeting.exe  &ndash;n 99<br \/>\nGreetings, World!<\/p>\n<h3>Summary<\/h3>\n<p>That about wraps up sending command line arguments to the application.  Thanks to Jon for providing support for NDesk on #mono.  You&#8217;re right.  I should read your <a href=\"http:\/\/www.ndesk.org\/doc\/ndesk-options\/\">docs<\/a> instead of bothering you!<\/p>\n<p>We now have a bit more complication in our build system and have something we can re-factor a bit.  In the next article, we&#8217;ll start to make use of some of the autotools bits.<\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=cjamescollier\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this installment, we will look at parsing command-line options. We&#8217;re going to use Jon Pryor&#8216;s NDesk.Options library. Since he has not made a .deb package yet, we&#8217;ll use curl to integrate NDesk.Options.cs into our codebase. Get back to our workspace $ cd ~\/src\/greeting Get curl $ sudo apt-get install curl Pull down a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[180,38,188,17,79,181,7,166],"tags":[281],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-autotools","category-c","category-cli","category-debian","category-free-software","category-irc","category-mono","category-software","tag-autotools"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1YDIB-31","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=187"}],"version-history":[{"count":1,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":191,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts\/187\/revisions\/191"}],"wp:attachment":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}