110 lines
7.5 KiB
HTML
110 lines
7.5 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<title>CLiki: unix-opts</title>
|
||
|
<link rel="alternate" type="application/atom+xml" title="ATOM feed of edits to current article"
|
||
|
href="https://www.cliki.net/site/feed/article.atom?title=unix-opts">
|
||
|
<link rel="stylesheet" href="static/css/style.css">
|
||
|
<link rel="stylesheet" href="static/css/colorize.css">
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
<span class="hidden">CLiki - unix-opts</span>
|
||
|
<div id="content"><div id="content-area"><div id="article-title">unix-opts</div><div id="article">This is a minimalistic parser of command line options. Main advantage of
|
||
|
this library is ability to concisely define command line options once
|
||
|
and then use this definition for parsing and extraction of command line
|
||
|
arguments, as well as printing description of command line options (you
|
||
|
get <code>--help</code> for free). This way you don't need to repeat yourself. Also,
|
||
|
<a href="unix-opts.html" class="internal">unix-opts</a> doesn't depend on anything and allows to precisely control
|
||
|
behavior of the parser via Common Lisp restarts.<p>GitHub repository: <a href="https://github.com/libre-man/unix-opts">https://github.com/libre-man/unix-opts</a><p>You can install it via Quicklisp:<p><div class="code"><span class="nonparen"><span class="paren1">(<span class="nonparen">ql:quickload <span class="string">"unix-opts"</span></span>)</span>
|
||
|
</span></div><p>Here is an example how to define some command line options with
|
||
|
<a href="unix-opts.html" class="internal">unix-opts</a>:<p><div class="code"><span class="nonparen"><span class="paren1">(<span class="nonparen"><i><span class="symbol">opts:define-opts</span></i>
|
||
|
<span class="paren2">(<span class="nonparen"><span class="keyword">:name</span> <span class="keyword">:output</span>
|
||
|
<span class="keyword">:description</span> <span class="string">"redirect output to file FILE"</span>
|
||
|
<span class="keyword">:short</span> <span class="character">#\o</span>
|
||
|
<span class="keyword">:long</span> <span class="string">"output"</span>
|
||
|
<span class="keyword">:arg-parser</span> #'<a href="https://www.cliki.net/site/HyperSpec/Body/fun_identity.html" class="symbol">identity</a>
|
||
|
<span class="keyword">:meta-var</span> <span class="string">"FILE"</span></span>)</span>
|
||
|
<span class="paren2">(<span class="nonparen"><span class="keyword">:name</span> <span class="keyword">:level</span>
|
||
|
<span class="keyword">:description</span> <span class="string">"the program will run on LEVEL level"</span>
|
||
|
<span class="keyword">:short</span> <span class="character">#\l</span>
|
||
|
<span class="keyword">:long</span> <span class="string">"level"</span>
|
||
|
<span class="keyword">:arg-parser</span> #'<a href="https://www.cliki.net/site/HyperSpec/Body/fun_parse-integer.html" class="symbol">parse-integer</a>
|
||
|
<span class="keyword">:meta-var</span> <span class="string">"LEVEL"</span></span>)</span>
|
||
|
<span class="paren2">(<span class="nonparen"><span class="keyword">:name</span> <span class="keyword">:verbose</span>
|
||
|
<span class="keyword">:description</span> <span class="string">"verbose output"</span>
|
||
|
<span class="keyword">:short</span> <span class="character">#\v</span>
|
||
|
<span class="keyword">:long</span> <span class="string">"verbose"</span></span>)</span>
|
||
|
<span class="paren2">(<span class="nonparen"><span class="keyword">:name</span> <span class="keyword">:help</span>
|
||
|
<span class="keyword">:description</span> <span class="string">"print this help text"</span>
|
||
|
<span class="keyword">:short</span> <span class="character">#\h</span>
|
||
|
<span class="keyword">:long</span> <span class="string">"help"</span></span>)</span></span>)</span></span></div><p>See it in action (<a href="https://github.com/libre-man/unix-opts/blob/master/example/example.lisp">full
|
||
|
source code of the example</a>):<p><pre>$ sh example.sh --help
|
||
|
example - program to demonstrate unix-opts library
|
||
|
|
||
|
Available options:
|
||
|
-h, --help print this help text
|
||
|
-v, --verbose verbose output
|
||
|
-l, --level LEVEL the program will run on LEVEL level
|
||
|
-o, --output FILE redirect output to file FILE
|
||
|
so that's how it works...
|
||
|
|
||
|
free args:
|
||
|
$ sh example.sh -v file1.txt file2.txt
|
||
|
OK, running in verbose mode...
|
||
|
free args: file1.txt, file2.txt
|
||
|
$ sh example.sh --level 10 --output foo.txt bar.txt
|
||
|
I see you've supplied level option, you want 10 level!
|
||
|
I see you want to output the stuff to "foo.txt"!
|
||
|
free args: bar.txt
|
||
|
$ sh example.sh --level kitty foo.txt
|
||
|
fatal: cannot parse "kitty" as argument of "--level"
|
||
|
free args:
|
||
|
$ sh example.sh --hoola-boola noola.txt
|
||
|
warning: "--hoola-boola" option is unknown!
|
||
|
free args: noola.txt
|
||
|
$ sh example.sh -vgl=10
|
||
|
warning: "-g" option is unknown!
|
||
|
OK, running in verbose mode...
|
||
|
I see you've supplied level option, you want 10 level!
|
||
|
free args:
|
||
|
</pre><p><a href="unix-opts.html" class="internal">unix-opts</a> was written by <a href="Mark Karpov.html" class="internal">Mark Karpov</a> and is maintained by libre-man.<p>It's distributed under <a href="MIT-license.html" class="internal">MIT-license</a>.<p><hr><p>Relevant topics: <a href="library.html" class="category">library</a>, <a href="console.html" class="category">console</a>, <a href="system programming.html" class="category">system programming</a>
|
||
|
<a href="command-line options parser.html" class="category">command-line options parser</a></div></div>
|
||
|
<div id="footer" class="buttonbar"><ul><li><a href="unix-opts.html">Current version</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/history?article=unix-opts">History</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/backlinks?article=unix-opts">Backlinks</a></li><li><a href="https://www.cliki.net/site/edit-article?title=unix-opts&from-revision=3772977984">Edit</a></li><li><a href="https://www.cliki.net/site/edit-article?create=t">Create</a></li></ul></div>
|
||
|
</div>
|
||
|
<div id="header-buttons" class="buttonbar">
|
||
|
<ul>
|
||
|
<li><a href="https://www.cliki.net/">Home</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/recent-changes">Recent Changes</a></li>
|
||
|
<li><a href="CLiki.html">About</a></li>
|
||
|
<li><a href="Text Formatting.html">Text Formatting</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/tools">Tools</a></li>
|
||
|
</ul>
|
||
|
<div id="search">
|
||
|
<form action="https://www.cliki.net/site/search">
|
||
|
<label for="search_query" class="hidden">Search CLiki</label>
|
||
|
<input type="text" name="query" id="search_query" value="" />
|
||
|
<input type="submit" value="search" />
|
||
|
</form>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="pageheader">
|
||
|
<div id="header">
|
||
|
<span id="logo">CLiki</span>
|
||
|
<span id="slogan">the common lisp wiki</span>
|
||
|
<div id="login"><form method="post" action="https://www.cliki.net/site/login">
|
||
|
<label for="login_name" class="hidden">Account name</label>
|
||
|
<input type="text" name="name" id="login_name" class="login_input" />
|
||
|
<label for= "login_password" class="hidden">Password</label>
|
||
|
<input type="password" name="password" id="login_password" class="login_input" />
|
||
|
<input type="submit" name="login" value="login" id="login_submit" /><br />
|
||
|
<div id="register"><a href="https://www.cliki.net/site/register">register</a></div>
|
||
|
<input type="submit" name="reset-pw" value="reset password" id="reset_pw" />
|
||
|
</form>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body></html>
|