{"id":4493,"date":"2022-08-23T18:17:14","date_gmt":"2022-08-23T23:17:14","guid":{"rendered":"https:\/\/dev.iachieved.it\/iachievedit\/?p=4493"},"modified":"2022-08-23T18:21:23","modified_gmt":"2022-08-23T23:21:23","slug":"practical-racket-argv","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/practical-racket-argv\/","title":{"rendered":"Practical Racket &#8211; argv"},"content":{"rendered":"<p>This is my first post featuring the <a href=\"https:\/\/racket-lang.org\/\">Racket<\/a> language.  At some point we may start evangelizing and looking down our nose at people who don&#8217;t leave in the superfluous parentheses, but for now let&#8217;s keep it light.  Even still, this is not a Racket or Lisp tutorial.  If the sight of <code>(sqrt (+ (* 3 4) (* 6 7))))<\/code> scares you, run away!<\/p>\n<p>Every time I learn a new language it is always the same:<\/p>\n<ul>\n<li>learn the basic syntax\n<li>search on how to concatenate a string\n<li>search on how to open and read a file\n<li>search on how to read and write JSON\n<\/ul>\n<p>And so on.  Invariably one also comes to &#8220;how do I read <code>argv<\/code>?&#8221;  This is that simple post for Racket.<\/p>\n<p>For the equivalent of Python&#8217;s <code>sys.argv[1]<\/code> or Perl&#8217;s <code>$ARGV[0]<\/code>, we can do this:<\/p>\n<pre class=\"toolbar:2 lang:lisp decode:true \" >\n#!\/usr\/bin\/env racket\n#lang racket\n\n(define fname (vector-ref (current-command-line-arguments) 0))\n\n(printf \"Opening file ~a\\n\" fname)<\/pre>\n<p>There&#8217;s a lot going on here if you&#8217;ve never read Lisp or Racket before.  <a href=\"https:\/\/docs.racket-lang.org\/reference\/runtime.html#%28def._%28%28quote._~23~25kernel%29._current-command-line-arguments%29%29\"><code>current-command-line-arguments<\/code><\/a> returns a <i>vector<\/i> (not a list) of the arguments passed on the command.  We only want the first argument, and Racket doesn&#8217;t provide the filename of the script, so the counting starts at zero.  <code>vector-ref<\/code> returns that first element.  We bind that to <code>fname<\/code> and there you have it.<\/p>\n<p>That&#8217;s nice and all, but what if there&#8217;s no argument supplied on the command line?  Let&#8217;s try this:<\/p>\n<pre class=\"toolbar:2 lang:lisp decode:true\" >\n#!\/usr\/bin\/env racket\n#lang racket\n\n(define fname (cond \n  [(= 0 (vector-length (current-command-line-arguments)))\n    (error \"No filename given!\")\n    (exit)]\n  [else (vector-ref (current-command-line-arguments) 0)]))\n\n(printf \"Opening file ~a...\\n\" fname)\n<\/pre>\n<p>Trickier, indeed.<\/p>\n<p>We&#8217;re still going to bind <code>fname<\/code>, and perhaps there is a bit of cheating with the use of <code>(exit)<\/code>, but with the <a href=\"https:\/\/docs.racket-lang.org\/reference\/if.html\"><code>cond<\/code> routine<\/a> we&#8217;ll test if the vector is zero length and if so, bail.  <i>Otherwise<\/i> the <code>else<\/code> clause is triggered and <code>fname<\/code> will get the value of the clause.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is my first post featuring the Racket language. At some point we may start evangelizing and looking down our nose at people who don&#8217;t leave in the superfluous parentheses, but for now let&#8217;s keep it light. Even still, this is not a Racket or Lisp tutorial. If the sight of (sqrt (+ (* 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[113],"tags":[],"class_list":["post-4493","post","type-post","status-publish","format-standard","hentry","category-racket"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4493"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=4493"}],"version-history":[{"count":8,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4493\/revisions"}],"predecessor-version":[{"id":4501,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4493\/revisions\/4501"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=4493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=4493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=4493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}