--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+<!-- 2023-10-12 Thu 23:05 -->
+<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
+<meta name="viewport" content="width=device-width, initial-scale=1" />
+<title>Commandline interface arguments processing</title>
+<meta name="author" content="Svjatoslav Agejenko" />
+<meta name="generator" content="Org Mode" />
+<style>
+ #content { max-width: 60em; margin: auto; }
+ .title { text-align: center;
+ margin-bottom: .2em; }
+ .subtitle { text-align: center;
+ font-size: medium;
+ font-weight: bold;
+ margin-top:0; }
+ .todo { font-family: monospace; color: red; }
+ .done { font-family: monospace; color: green; }
+ .priority { font-family: monospace; color: orange; }
+ .tag { background-color: #eee; font-family: monospace;
+ padding: 2px; font-size: 80%; font-weight: normal; }
+ .timestamp { color: #bebebe; }
+ .timestamp-kwd { color: #5f9ea0; }
+ .org-right { margin-left: auto; margin-right: 0px; text-align: right; }
+ .org-left { margin-left: 0px; margin-right: auto; text-align: left; }
+ .org-center { margin-left: auto; margin-right: auto; text-align: center; }
+ .underline { text-decoration: underline; }
+ #postamble p, #preamble p { font-size: 90%; margin: .2em; }
+ p.verse { margin-left: 3%; }
+ pre {
+ border: 1px solid #e6e6e6;
+ border-radius: 3px;
+ background-color: #f2f2f2;
+ padding: 8pt;
+ font-family: monospace;
+ overflow: auto;
+ margin: 1.2em;
+ }
+ pre.src {
+ position: relative;
+ overflow: auto;
+ }
+ pre.src:before {
+ display: none;
+ position: absolute;
+ top: -8px;
+ right: 12px;
+ padding: 3px;
+ color: #555;
+ background-color: #f2f2f299;
+ }
+ pre.src:hover:before { display: inline; margin-top: 14px;}
+ /* Languages per Org manual */
+ pre.src-asymptote:before { content: 'Asymptote'; }
+ pre.src-awk:before { content: 'Awk'; }
+ pre.src-authinfo::before { content: 'Authinfo'; }
+ pre.src-C:before { content: 'C'; }
+ /* pre.src-C++ doesn't work in CSS */
+ pre.src-clojure:before { content: 'Clojure'; }
+ pre.src-css:before { content: 'CSS'; }
+ pre.src-D:before { content: 'D'; }
+ pre.src-ditaa:before { content: 'ditaa'; }
+ pre.src-dot:before { content: 'Graphviz'; }
+ pre.src-calc:before { content: 'Emacs Calc'; }
+ pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
+ pre.src-fortran:before { content: 'Fortran'; }
+ pre.src-gnuplot:before { content: 'gnuplot'; }
+ pre.src-haskell:before { content: 'Haskell'; }
+ pre.src-hledger:before { content: 'hledger'; }
+ pre.src-java:before { content: 'Java'; }
+ pre.src-js:before { content: 'Javascript'; }
+ pre.src-latex:before { content: 'LaTeX'; }
+ pre.src-ledger:before { content: 'Ledger'; }
+ pre.src-lisp:before { content: 'Lisp'; }
+ pre.src-lilypond:before { content: 'Lilypond'; }
+ pre.src-lua:before { content: 'Lua'; }
+ pre.src-matlab:before { content: 'MATLAB'; }
+ pre.src-mscgen:before { content: 'Mscgen'; }
+ pre.src-ocaml:before { content: 'Objective Caml'; }
+ pre.src-octave:before { content: 'Octave'; }
+ pre.src-org:before { content: 'Org mode'; }
+ pre.src-oz:before { content: 'OZ'; }
+ pre.src-plantuml:before { content: 'Plantuml'; }
+ pre.src-processing:before { content: 'Processing.js'; }
+ pre.src-python:before { content: 'Python'; }
+ pre.src-R:before { content: 'R'; }
+ pre.src-ruby:before { content: 'Ruby'; }
+ pre.src-sass:before { content: 'Sass'; }
+ pre.src-scheme:before { content: 'Scheme'; }
+ pre.src-screen:before { content: 'Gnu Screen'; }
+ pre.src-sed:before { content: 'Sed'; }
+ pre.src-sh:before { content: 'shell'; }
+ pre.src-sql:before { content: 'SQL'; }
+ pre.src-sqlite:before { content: 'SQLite'; }
+ /* additional languages in org.el's org-babel-load-languages alist */
+ pre.src-forth:before { content: 'Forth'; }
+ pre.src-io:before { content: 'IO'; }
+ pre.src-J:before { content: 'J'; }
+ pre.src-makefile:before { content: 'Makefile'; }
+ pre.src-maxima:before { content: 'Maxima'; }
+ pre.src-perl:before { content: 'Perl'; }
+ pre.src-picolisp:before { content: 'Pico Lisp'; }
+ pre.src-scala:before { content: 'Scala'; }
+ pre.src-shell:before { content: 'Shell Script'; }
+ pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
+ /* additional language identifiers per "defun org-babel-execute"
+ in ob-*.el */
+ pre.src-cpp:before { content: 'C++'; }
+ pre.src-abc:before { content: 'ABC'; }
+ pre.src-coq:before { content: 'Coq'; }
+ pre.src-groovy:before { content: 'Groovy'; }
+ /* additional language identifiers from org-babel-shell-names in
+ ob-shell.el: ob-shell is the only babel language using a lambda to put
+ the execution function name together. */
+ pre.src-bash:before { content: 'bash'; }
+ pre.src-csh:before { content: 'csh'; }
+ pre.src-ash:before { content: 'ash'; }
+ pre.src-dash:before { content: 'dash'; }
+ pre.src-ksh:before { content: 'ksh'; }
+ pre.src-mksh:before { content: 'mksh'; }
+ pre.src-posh:before { content: 'posh'; }
+ /* Additional Emacs modes also supported by the LaTeX listings package */
+ pre.src-ada:before { content: 'Ada'; }
+ pre.src-asm:before { content: 'Assembler'; }
+ pre.src-caml:before { content: 'Caml'; }
+ pre.src-delphi:before { content: 'Delphi'; }
+ pre.src-html:before { content: 'HTML'; }
+ pre.src-idl:before { content: 'IDL'; }
+ pre.src-mercury:before { content: 'Mercury'; }
+ pre.src-metapost:before { content: 'MetaPost'; }
+ pre.src-modula-2:before { content: 'Modula-2'; }
+ pre.src-pascal:before { content: 'Pascal'; }
+ pre.src-ps:before { content: 'PostScript'; }
+ pre.src-prolog:before { content: 'Prolog'; }
+ pre.src-simula:before { content: 'Simula'; }
+ pre.src-tcl:before { content: 'tcl'; }
+ pre.src-tex:before { content: 'TeX'; }
+ pre.src-plain-tex:before { content: 'Plain TeX'; }
+ pre.src-verilog:before { content: 'Verilog'; }
+ pre.src-vhdl:before { content: 'VHDL'; }
+ pre.src-xml:before { content: 'XML'; }
+ pre.src-nxml:before { content: 'XML'; }
+ /* add a generic configuration mode; LaTeX export needs an additional
+ (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
+ pre.src-conf:before { content: 'Configuration File'; }
+
+ table { border-collapse:collapse; }
+ caption.t-above { caption-side: top; }
+ caption.t-bottom { caption-side: bottom; }
+ td, th { vertical-align:top; }
+ th.org-right { text-align: center; }
+ th.org-left { text-align: center; }
+ th.org-center { text-align: center; }
+ td.org-right { text-align: right; }
+ td.org-left { text-align: left; }
+ td.org-center { text-align: center; }
+ dt { font-weight: bold; }
+ .footpara { display: inline; }
+ .footdef { margin-bottom: 1em; }
+ .figure { padding: 1em; }
+ .figure p { text-align: center; }
+ .equation-container {
+ display: table;
+ text-align: center;
+ width: 100%;
+ }
+ .equation {
+ vertical-align: middle;
+ }
+ .equation-label {
+ display: table-cell;
+ text-align: right;
+ vertical-align: middle;
+ }
+ .inlinetask {
+ padding: 10px;
+ border: 2px solid gray;
+ margin: 10px;
+ background: #ffffcc;
+ }
+ #org-div-home-and-up
+ { text-align: right; font-size: 70%; white-space: nowrap; }
+ textarea { overflow-x: auto; }
+ .linenr { font-size: smaller }
+ .code-highlighted { background-color: #ffff00; }
+ .org-info-js_info-navigation { border-style: none; }
+ #org-info-js_console-label
+ { font-size: 10px; font-weight: bold; white-space: nowrap; }
+ .org-info-js_search-highlight
+ { background-color: #ffff00; color: #000000; font-weight: bold; }
+ .org-svg { }
+</style>
+<link rel="stylesheet" type="text/css" href="https://thomasf.github.io/solarized-css/solarized-dark.min.css" />
+</head>
+<body>
+<div id="content" class="content">
+<h1 class="title">Commandline interface arguments processing</h1>
+<div id="table-of-contents" role="doc-toc">
+<h2>Table of Contents</h2>
+<div id="text-table-of-contents" role="doc-toc">
+<ul>
+<li><a href="#orga388043">1. Terminology</a>
+<ul>
+<li><a href="#orgc364e3e">1.1. Command and argument</a></li>
+<li><a href="#org607ea14">1.2. Subcommand</a></li>
+<li><a href="#org3f1a445">1.3. Option</a></li>
+<li><a href="#orgf5c6947">1.4. Parameter</a></li>
+</ul>
+</li>
+</ul>
+</div>
+</div>
+<ul class="org-ul">
+<li><a href="index.html#ID-bb4f96cd-458c-495b-a605-313b2e3e28d2">Back to CLI Helper - library main page</a></li>
+</ul>
+
+<div id="outline-container-orga388043" class="outline-2">
+<h2 id="orga388043"><span class="section-number-2">1.</span> Terminology</h2>
+<div class="outline-text-2" id="text-1">
+</div>
+<div id="outline-container-orgc364e3e" class="outline-3">
+<h3 id="orgc364e3e"><span class="section-number-3">1.1.</span> Command and argument</h3>
+<div class="outline-text-3" id="text-1-1">
+<p>
+Every command-line application has a way of receiving input from
+users, usually in the form of command-line arguments. A command-line
+argument is a piece of information provided to the command-line
+application when it's invoked. These arguments are provided as an
+array of strings. The first element of the array (argument 0) is
+typically the name of the command itself.
+</p>
+
+<p>
+In the example below, 'my-video-coder' is our command, and the rest
+are arguments:
+</p>
+
+<div class="org-src-container">
+<pre class="src src-shell">my-video-coder encode --input vid1.mp4 vid2.mp4 vid3.mp4 --quality 5
+</pre>
+</div>
+
+<p>
+To better understand how these concepts work together, let's break
+down our example command:
+</p>
+
+<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
+
+
+<colgroup>
+<col class="org-right" />
+
+<col class="org-left" />
+
+<col class="org-left" />
+</colgroup>
+<thead>
+<tr>
+<th scope="col" class="org-right">argument #</th>
+<th scope="col" class="org-left">value(s)</th>
+<th scope="col" class="org-left">type</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td class="org-right">0</td>
+<td class="org-left">my-video-coder</td>
+<td class="org-left">command</td>
+</tr>
+
+<tr>
+<td class="org-right">1</td>
+<td class="org-left">encode</td>
+<td class="org-left"><a href="#org607ea14">subcommand</a></td>
+</tr>
+
+<tr>
+<td class="org-right">2</td>
+<td class="org-left">–input</td>
+<td class="org-left"><a href="#org3f1a445">option1</a></td>
+</tr>
+
+<tr>
+<td class="org-right">3, 4, 5</td>
+<td class="org-left">vid1.mp4 vid2.mp4 vid3.mp4</td>
+<td class="org-left"><a href="#orgf5c6947">parameters for –input option</a></td>
+</tr>
+
+<tr>
+<td class="org-right">6</td>
+<td class="org-left">–quality</td>
+<td class="org-left"><a href="#org3f1a445">option2</a></td>
+</tr>
+
+<tr>
+<td class="org-right">7</td>
+<td class="org-left">5</td>
+<td class="org-left"><a href="#orgf5c6947">parameter for –quaily option</a></td>
+</tr>
+</tbody>
+</table>
+</div>
+</div>
+
+<div id="outline-container-org607ea14" class="outline-3">
+<h3 id="org607ea14"><span class="section-number-3">1.2.</span> Subcommand</h3>
+<div class="outline-text-3" id="text-1-2">
+<p>
+Subcommands are arguments that invoke more specific action that a
+command can perform. They are often used with commands that have
+multiple functions. In our example, <b>encode</b> is a subcommand of
+<b>my-video-coder</b>.
+</p>
+</div>
+</div>
+
+<div id="outline-container-org3f1a445" class="outline-3">
+<h3 id="org3f1a445"><span class="section-number-3">1.3.</span> Option</h3>
+<div class="outline-text-3" id="text-1-3">
+<p>
+Options are arguments that change the behavior of a command or
+subcommand. They usually start with a dash (-) or double dash
+(–). For instance, <b>–input</b> and <b>–quality</b> are options in our
+example command.
+</p>
+</div>
+</div>
+
+<div id="outline-container-orgf5c6947" class="outline-3">
+<h3 id="orgf5c6947"><span class="section-number-3">1.4.</span> Parameter</h3>
+<div class="outline-text-3" id="text-1-4">
+<p>
+Parameter provides additional information to a command, subcommand or
+option.
+</p>
+
+<p>
+For instance, in our example:
+</p>
+<ul class="org-ul">
+<li>'vid1.mp4 vid2.mp4 vid3.mp4' are parameters for the <b>–input</b> option.</li>
+<li>'5' is a parameter for the <b>–quality</b> option.</li>
+</ul>
+</div>
+</div>
+</div>
+</div>
+<div id="postamble" class="status">
+<p class="author">Author: Svjatoslav Agejenko</p>
+<p class="date">Created: 2023-10-12 Thu 23:05</p>
+<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
+</div>
+</body>
+</html>
- [[id:bb4f96cd-458c-495b-a605-313b2e3e28d2][Back to CLI Helper - library main page]]
-* Theory
-** Commands and Arguments
+* Terminology
+** Command and argument
Every command-line application has a way of receiving input from
users, usually in the form of command-line arguments. A command-line
| 2 | --input | [[id:ffedf388-4d23-41eb-98d0-83fd3940b24d][option1]] |
| 3, 4, 5 | vid1.mp4 vid2.mp4 vid3.mp4 | [[id:8a39d20c-421f-4bc7-94e4-8e561e58bea0][parameters for --input option]] |
| 6 | --quality | [[id:ffedf388-4d23-41eb-98d0-83fd3940b24d][option2]] |
-| 7 | 5 | [[id:8a39d20c-421f-4bc7-94e4-8e561e58bea0][option for --quaily option]] |
+| 7 | 5 | [[id:8a39d20c-421f-4bc7-94e4-8e561e58bea0][parameter for --quaily option]] |
** Subcommand
:PROPERTIES:
:ID: 94242e8a-c59b-42fd-8cc7-ba3df1938119
:END:
-Subcommands are more specific actions that a command can perform. They
-are often used with commands that have multiple functions. In our
-example, *encode* is a subcommand of *my-video-coder*.
+Subcommands are arguments that invoke more specific action that a
+command can perform. They are often used with commands that have
+multiple functions. In our example, *encode* is a subcommand of
+*my-video-coder*.
** Option
:PROPERTIES:
For instance, in our example:
- 'vid1.mp4 vid2.mp4 vid3.mp4' are parameters for the *--input* option.
- '5' is a parameter for the *--quality* option.
-
-* Implementation :noexport:
-
-Parsing Command-line Arguments:
-- `Parameter` class is used for defining parameters with their
- descriptions, types, and aliases. It also keeps track of whether the
- specific option was present in the command line or not. This
- information is used later in the processing.
-
- - `DirectoryParameter`, `FileParameter`, `IntegerParameter`,
- `StringParameter`, `NullParameter`, and `StringParameters` are
- examples of Parameter classes, each one having unique
- characteristics for handling specific types of parameters
- (directories, files, integers, strings, etc.).
-
-- `ArgumentCount` class determines if a option can have any amount
- of arguments (MULTI), exactly one argument (SINGLE), or no arguments
- (NONE).
-
-- `Parser` class takes all these parameters and checks whether all
- required arguments are provided and if they match the expected
- format.
-
-** Usage example
-
-TODO:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
-<!-- 2022-07-13 Wed 04:24 -->
+<!-- 2023-10-12 Thu 22:18 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>CLI Helper - library to help implementing commandline interfaces</title>
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
-<li><a href="#org5be2417">1. General</a>
+<li><a href="#orgf8634f9">1. General</a>
<ul>
-<li><a href="#org3a8d88b">1.1. Source code</a></li>
+<li><a href="#orgd17f1cf">1.1. Source code</a></li>
</ul>
</li>
-<li><a href="#org8bb96fe">2. Library contents</a></li>
-<li><a href="#org93b7ead">3. Usage</a></li>
+<li><a href="#org873ac39">2. Library contents</a>
+<ul>
+<li><a href="#org7e47dc1">2.1. Ask for user input</a></li>
+</ul>
+</li>
+<li><a href="#orge7ef3ef">3. Getting the library</a></li>
</ul>
</div>
</div>
-<div id="outline-container-org5be2417" class="outline-2">
-<h2 id="org5be2417"><span class="section-number-2">1.</span> General</h2>
+<div id="outline-container-orgf8634f9" class="outline-2">
+<h2 id="orgf8634f9"><span class="section-number-2">1.</span> General</h2>
<div class="outline-text-2" id="text-1">
<ul class="org-ul">
<li>This program is free software: released under Creative Commons Zero
</ul>
</div>
-<div id="outline-container-org3a8d88b" class="outline-3">
-<h3 id="org3a8d88b"><span class="section-number-3">1.1.</span> Source code</h3>
+<div id="outline-container-orgd17f1cf" class="outline-3">
+<h3 id="orgd17f1cf"><span class="section-number-3">1.1.</span> Source code</h3>
<div class="outline-text-3" id="text-1-1">
<ul class="org-ul">
<li><a href="https://www2.svjatoslav.eu/gitweb/?p=cli-helper.git;a=snapshot;h=HEAD;sf=tgz">Download latest snapshot in TAR GZ format</a></li>
</div>
</div>
-<div id="outline-container-org8bb96fe" class="outline-2">
-<h2 id="org8bb96fe"><span class="section-number-2">2.</span> Library contents</h2>
+<div id="outline-container-org873ac39" class="outline-2">
+<h2 id="org873ac39"><span class="section-number-2">2.</span> Library contents</h2>
<div class="outline-text-2" id="text-2">
<ul class="org-ul">
-<li>See <a href="https://www3.svjatoslav.eu/projects/svjatoslav_commons/apidocs/">JavaDoc</a>.</li>
+<li>See also: <a href="https://www3.svjatoslav.eu/projects/svjatoslav_commons/apidocs/">CLI Helper JavaDoc</a>.</li>
+</ul>
-<li><p>
-Commandline Interface helper
+<p>
+This library is a collection of command-line interface (CLI) helper
+functions that simplifies the process of building and maintaining CLI
+applications. The library provides several different functionalities,
+such as:
</p>
-<pre class="example">
-eu.svjatoslav.commons.cli_helper.CLIHelper
-</pre></li>
-<li><p>
-Commandline arguments parser and validator.
-</p>
-<pre class="example">
-eu.svjatoslav.commons.cli_helper.parameter_parser
-</pre></li>
+<ul class="org-ul">
+<li><a href="#org7e47dc1">Asking for user input</a></li>
+<li><a href="CLI arguments processing.html#ID-46115263-ed3d-4acc-9ec5-523d7acf87b8">Commandline interface arguments processing</a></li>
</ul>
</div>
+
+<div id="outline-container-org7e47dc1" class="outline-3">
+<h3 id="org7e47dc1"><span class="section-number-3">2.1.</span> Ask for user input</h3>
+<div class="outline-text-3" id="text-2-1">
+<dl class="org-dl">
+<dt>askBoolean()</dt><dd>Asks the user to enter a boolean value (yes/no).</dd>
+<dt>askLong()</dt><dd>Asks the user to enter an integer.</dd>
+<dt>askString()</dt><dd>Asks the user to enter a string.</dd>
+</dl>
+</div>
+</div>
</div>
-<div id="outline-container-org93b7ead" class="outline-2">
-<h2 id="org93b7ead"><span class="section-number-2">3.</span> Usage</h2>
+<div id="outline-container-orge7ef3ef" class="outline-2">
+<h2 id="orge7ef3ef"><span class="section-number-2">3.</span> Getting the library</h2>
<div class="outline-text-2" id="text-3">
<p>
-Instructions to embed svjatoslav-commons in your project as a library:
+Instructions to embed svjatoslav-commons library in your project:
</p>
<p>
</p>
<div class="org-src-container">
-<pre class="src src-xml"><<span style="color: #A6E22E;">dependencies</span>>
+<pre class="src src-xml"><<span style="color: #89b4fa;">dependencies</span>>
...
- <<span style="color: #A6E22E;">dependency</span>>
- <<span style="color: #A6E22E;">groupId</span>>eu.svjatoslav</<span style="color: #A6E22E;">groupId</span>>
- <<span style="color: #A6E22E;">artifactId</span>>cli-helper</<span style="color: #A6E22E;">artifactId</span>>
- <<span style="color: #A6E22E;">version</span>>1.0</<span style="color: #A6E22E;">version</span>>
- </<span style="color: #A6E22E;">dependency</span>>
+ <<span style="color: #89b4fa;">dependency</span>>
+ <<span style="color: #89b4fa;">groupId</span>>eu.svjatoslav</<span style="color: #89b4fa;">groupId</span>>
+ <<span style="color: #89b4fa;">artifactId</span>>cli-helper</<span style="color: #89b4fa;">artifactId</span>>
+ <<span style="color: #89b4fa;">version</span>>1.0</<span style="color: #89b4fa;">version</span>>
+ </<span style="color: #89b4fa;">dependency</span>>
...
-</<span style="color: #A6E22E;">dependencies</span>>
+</<span style="color: #89b4fa;">dependencies</span>>
-<<span style="color: #A6E22E;">repositories</span>>
+<<span style="color: #89b4fa;">repositories</span>>
...
- <<span style="color: #A6E22E;">repository</span>>
- <<span style="color: #A6E22E;">id</span>>svjatoslav.eu</<span style="color: #A6E22E;">id</span>>
- <<span style="color: #A6E22E;">name</span>>Svjatoslav repository</<span style="color: #A6E22E;">name</span>>
- <<span style="color: #A6E22E;">url</span>>http://www3.svjatoslav.eu/maven/</<span style="color: #A6E22E;">url</span>>
- </<span style="color: #A6E22E;">repository</span>>
+ <<span style="color: #89b4fa;">repository</span>>
+ <<span style="color: #89b4fa;">id</span>>svjatoslav.eu</<span style="color: #89b4fa;">id</span>>
+ <<span style="color: #89b4fa;">name</span>>Svjatoslav repository</<span style="color: #89b4fa;">name</span>>
+ <<span style="color: #89b4fa;">url</span>>http://www3.svjatoslav.eu/maven/</<span style="color: #89b4fa;">url</span>>
+ </<span style="color: #89b4fa;">repository</span>>
...
-</<span style="color: #A6E22E;">repositories</span>>
+</<span style="color: #89b4fa;">repositories</span>>
</pre>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Svjatoslav Agejenko</p>
-<p class="date">Created: 2022-07-13 Wed 04:24</p>
+<p class="date">Created: 2023-10-12 Thu 22:18</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser;
-
-public enum ArgumentCount {
-
- /**
- * Parameter has no arguments.
- */
- NONE,
- /**
- * Parameter has exactly one argument.
- */
- SINGLE,
-
- /**
- * Parameter can have any amount of arguments.
- */
- MULTI
-}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount.NONE;
+import static java.lang.String.join;
+import static java.util.Collections.addAll;
+import static java.util.stream.Collectors.joining;
+
+public abstract class Option<T, I extends Option> {
+
+ /**
+ * Purpose of this option, like: input image path, compression level, etc...
+ *
+ * Note: for describing option type there is {@link #describeFormat()}.
+ */
+ public final String description;
+
+ /**
+ * List of arguments for this parameter.
+ */
+ public final List<String> parameters = new ArrayList<>();
+ final ParameterCount parameterCount;
+
+ /**
+ * List of aliases for this option.
+ */
+ private final List<String> aliases = new ArrayList<>();
+
+ /**
+ * Indicates that at least one parameter is mandatory for this option.
+ */
+ protected boolean mandatory;
+
+ /**
+ * If this parameter was present in the commandline, then this boolean will
+ * be set to <code>true</code>.
+ */
+ private boolean isPresent;
+
+ public Option(final boolean mandatory,
+ final ParameterCount parameterCount, final String description,
+ final String... aliases2) {
+
+ this.mandatory = mandatory;
+ this.description = description;
+ this.parameterCount = parameterCount;
+
+ // save aliases
+ addAll(aliases, aliases2);
+ }
+
+ public Option(final String description, final ParameterCount parameterCount) {
+ this.description = description;
+ this.parameterCount = parameterCount;
+ }
+
+ @SuppressWarnings("unchecked")
+ public I addAliases(final String... aliasArray) {
+
+ // save aliases
+ addAll(aliases, aliasArray);
+
+ return (I) this;
+ }
+
+ /**
+ * @param parameterString parameter to add
+ * @return <code>true</code> if no errors were found. <code>false</code>
+ * otherwise.
+ */
+ public boolean addParameter(final String parameterString) {
+ // check if arguments are allowed for this parameter
+ if (parameterCount.equals(NONE)) {
+ System.out
+ .println("Error! No parameters are allowed for option(s): "
+ + getAliasesAsString());
+ return false;
+ }
+
+ // check if multiple arguments are allowed
+ if ((!parameters.isEmpty())
+ && (parameterCount.equals(ParameterCount.SINGLE))) {
+ System.out
+ .println("Error! Only single parameter is allowed for argument(s): "
+ + getAliasesAsString());
+ return false;
+ }
+
+ if (isValid(parameterString)) {
+ parameters.add(parameterString);
+ return true;
+ } else {
+ System.out.println("Error! Invalid parameter type for \"" + parameterString
+ + "\". It shall be " + describeFormat() + ".");
+ return false;
+ }
+ }
+
+ /**
+ * @return Single line parameter type description. For example: "file", "date", "integer", "regular expression", etc..
+ * <p>
+ * Note: for argument purpose description there is {@link #description}
+ */
+ public abstract String describeFormat();
+
+ /**
+ * @return aliases for this parameter as string.
+ */
+ public String getAliasesAsString() {
+ return join(", ", aliases);
+ }
+
+ /**
+ * @return help for this option.
+ */
+ public String getHelp() {
+ final StringBuilder result = new StringBuilder();
+
+ // first line
+ result.append(getAliasesAsString());
+ if (!NONE.equals(parameterCount)) {
+ result
+ .append(" (")
+ .append(isMandatory() ? "mandatory, " : "")
+ .append(describeFormat())
+ .append(")");
+
+ if (parameterCount.equals(ParameterCount.MULTI))
+ result.append("...");
+ }
+ result.append("\n");
+
+ // second line
+ result.append(" " + description + "\n");
+
+ return result.toString();
+ }
+
+ public abstract T getValue();
+
+ public boolean isMandatory() {
+ return mandatory;
+ }
+
+ /**
+ * @return the parameterSpecified
+ */
+ public boolean isPresent() {
+ return isPresent;
+ }
+
+ /**
+ * @param present the parameterSpecified to set
+ */
+ protected void setPresent(final boolean present) {
+ this.isPresent = present;
+ }
+
+ /**
+ * @param alias alias to check against
+ * @return <code>true</code> if given alias is registered for this
+ * parameter.
+ */
+ public boolean matchesAlias(final String alias) {
+ return aliases.contains(alias);
+
+ }
+
+ /**
+ * Notifies this option that no more parameters will follow.
+ * This gives option chance to verify if this is ok.
+ *
+ * @return <code>true</code> if no errors were found. <code>false</code>
+ * otherwise.
+ */
+ public boolean noMoreArguments() {
+
+ if ((!parameterCount.equals(NONE))
+ && (parameters.isEmpty())) {
+
+ System.out.println("Error! " + getAliasesAsString()
+ + " require at least one parameter.");
+
+ return false;
+ }
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ public I setMandatory() {
+ mandatory = true;
+ return (I) this;
+ }
+
+ /**
+ * @param value value to validate
+ * @return <code>true</code> if value is correct, <code>false</code>
+ * otherwise.
+ */
+ public abstract boolean isValid(String value);
+
+}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-public abstract class Parameter<T, I extends Parameter> {
-
- /**
- * Purpose of this argument, like: input image path, compression level, etc...
- *
- * Note: for describing argument type (file, integer, ...) there is {@link #describeFormat()}.
- */
- public final String description;
-
- /**
- * List of arguments for this parameter.
- */
- public final List<String> arguments = new ArrayList<>();
- final ArgumentCount argumentCount;
- private final List<String> aliases = new ArrayList<>();
- /**
- * Indicates that at least one argument is mandatory for this parameter.
- */
- protected boolean mandatory;
- /**
- * If this parameter was present in the commandline, then this boolean will
- * be set to <code>true</code>.
- */
- private boolean specified;
-
- public Parameter(final boolean mandatory,
- final ArgumentCount argumentCount, final String description,
- final String... aliases2) {
-
- this.mandatory = mandatory;
- this.description = description;
- this.argumentCount = argumentCount;
-
- // save aliases
- Collections.addAll(aliases, aliases2);
-
- }
-
- public Parameter(final String description, final ArgumentCount argumentCount) {
- this.description = description;
- this.argumentCount = argumentCount;
- }
-
- @SuppressWarnings("unchecked")
- public I addAliases(final String... aliasArray) {
-
- // save aliases
- Collections.addAll(aliases, aliasArray);
-
- return (I) this;
- }
-
- /**
- * @param argumentString argument to add
- * @return <code>true</code> if no errors were found. <code>false</code>
- * otherwise.
- */
- public boolean addArgument(final String argumentString) {
- // check if arguments are allowed for this parameter
- if (argumentCount.equals(ArgumentCount.NONE)) {
- System.out
- .println("Error! No arguments are allowed for parameters: "
- + getAliases());
- return false;
- }
-
- // check if multiple arguments are allowed
- if ((arguments.size() > 0)
- && (argumentCount.equals(ArgumentCount.SINGLE))) {
- System.out
- .println("Error! Only single argument is allowed for parameters: "
- + getAliases());
- return false;
- }
-
- if (!validate(argumentString)) {
-
- System.out.println("Error! Invalid argument \"" + argumentString
- + "\". It shall be " + describeFormat() + ".");
- return false;
-
- }
-
- arguments.add(argumentString);
-
- return true;
- }
-
- /**
- * @return Single line argument type description. If argument type should be file,
- * date, integer, regular expression, etc..
- *
- * Note: for argument purpose description there is {@link #description}
- */
- public abstract String describeFormat();
-
- public String getAliases() {
- final StringBuilder buffer = new StringBuilder();
-
- for (final String alias : aliases) {
-
- if (buffer.length() > 0)
- buffer.append(", ");
-
- buffer.append(alias);
- }
-
- return buffer.toString();
- }
-
- public String getHelp() {
- final StringBuilder buffer = new StringBuilder();
-
- // first line
- buffer.append(getAliases());
- if (!argumentCount.equals(ArgumentCount.NONE)) {
- buffer
- .append(" (")
- .append(isMandatory() ? "mandatory, " : "")
- .append(describeFormat())
- .append(")");
-
- if (argumentCount.equals(ArgumentCount.MULTI))
- buffer.append("...");
- }
- buffer.append("\n");
-
- // second line
- buffer.append(" " + description + "\n");
-
- return buffer.toString();
- }
-
- public abstract T getValue();
-
- public boolean isMandatory() {
- return mandatory;
- }
-
- /**
- * @return the parameterSpecified
- */
- public boolean isSpecified() {
- return specified;
- }
-
- /**
- * @param specified the parameterSpecified to set
- */
- protected void setSpecified(final boolean specified) {
- this.specified = specified;
- }
-
- /**
- * @param alias alias to check against
- * @return <code>true</code> if given alias is registered for this
- * parameter.
- */
- public boolean matchesAlias(final String alias) {
- return aliases.contains(alias);
-
- }
-
- /**
- * Notifies this parameter that no more arguments will follow. This gives
- * parameter chance to verify if this is ok.
- *
- * @return <code>true</code> if no errors were found. <code>false</code>
- * otherwise.
- */
- public boolean noMoreArguments() {
-
- if ((!argumentCount.equals(ArgumentCount.NONE))
- && (arguments.isEmpty())) {
-
- System.out.println("Error! " + getAliases()
- + " require at least one following argument.");
-
- return false;
- }
- return true;
- }
-
- @SuppressWarnings("unchecked")
- public I setMandatory() {
- mandatory = true;
- return (I) this;
- }
-
- /**
- * @param value value to validate
- * @return <code>true</code> if value is correct, <code>false</code>
- * otherwise.
- */
- public abstract boolean validate(String value);
-
-}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser;
+
+/**
+ * This enum is used to define how many parameters con be given to particular option.
+ */
+public enum ParameterCount {
+
+ /**
+ * Option has no parameters.
+ */
+ NONE,
+ /**
+ * Option has exactly one parameter.
+ */
+ SINGLE,
+
+ /**
+ * Option can have any amount of parameters.
+ */
+ MULTI
+}
*/
public class Parser {
- private final ArrayList<Parameter<?, ? extends Parameter>> parameters = new ArrayList<>();
+ private final ArrayList<Option<?, ? extends Option>> options = new ArrayList<>();
- public <E extends Parameter> E add(final E parameter) {
- parameters.add(parameter);
+ public <E extends Option> E add(final E parameter) {
+ options.add(parameter);
return parameter;
}
*/
private boolean checkMandatoryArgumentsPresent() {
- for (final Parameter parameter : parameters)
- if (parameter.isMandatory() && (!parameter.isSpecified())) {
+ for (final Option option : options)
+ if (option.isMandatory() && (!option.isPresent())) {
System.out.println("Error! Mandatory parameter ("
- + parameter.getAliases() + ") is not specified.");
+ + option.getAliasesAsString() + ") is not specified.");
return false;
}
* @param alias parameter alias
* @return found parameter or <code>null</code> if parameter was not found.
*/
- public Parameter findParameterByAlias(final String alias) {
+ public Option findParameterByAlias(final String alias) {
- for (final Parameter parameter : parameters)
- if (parameter.matchesAlias(alias))
- return parameter;
+ for (final Option option : options)
+ if (option.matchesAlias(alias))
+ return option;
return null;
}
*/
public boolean parse(final String[] args) {
- Parameter currentParameter = null;
+ Option currentOption = null;
for (final String argument : args) {
- final Parameter parameterForAlias = findParameterByAlias(argument);
- if (parameterForAlias == null) {
- if (currentParameter == null) {
+ final Option optionForAlias = findParameterByAlias(argument);
+ if (optionForAlias == null) {
+ if (currentOption == null) {
System.out.println("Unknown commandline parameter: "
+ argument);
return false;
}
- if (!currentParameter.addArgument(argument))
+ if (!currentOption.addParameter(argument))
return false;
} else {
- if (currentParameter != null)
- if (!currentParameter.noMoreArguments())
+ if (currentOption != null)
+ if (!currentOption.noMoreArguments())
return false;
- parameterForAlias.setSpecified(true);
- currentParameter = parameterForAlias;
+ optionForAlias.setPresent(true);
+ currentOption = optionForAlias;
}
}
public void showHelp() {
System.out.println("Available commandline arguments:");
- for (final Parameter parameter : parameters)
- System.out.println(parameter.getHelp());
+ for (final Option option : options)
+ System.out.println(option.getHelp());
}
}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+import java.io.File;
+
+public class DirectoryOption extends Option<File, DirectoryOption> {
+
+ private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
+
+ public DirectoryOption(final String description) {
+ super(description, ParameterCount.SINGLE);
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return existenceType.description + " directory";
+ }
+
+ @Override
+ public File getValue() {
+
+ if (parameters.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+
+ return new File(parameters.get(0));
+ }
+
+ public DirectoryOption mustExist() {
+ existenceType = ExistenceType.MUST_EXIST;
+ return this;
+ }
+
+ public DirectoryOption mustNotExist() {
+ existenceType = ExistenceType.MUST_NOT_EXIST;
+ return this;
+ }
+
+ @Override
+ public boolean isValid(final java.lang.String value) {
+ final File file = new File(value);
+
+ if (existenceType == ExistenceType.MUST_EXIST) {
+ return file.exists() && file.isDirectory();
+ }
+
+ if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+ return !file.exists();
+ }
+
+ if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isFile())
+ return false;
+
+ return true;
+ }
+
+ return false;
+ }
+
+}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+import java.io.File;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class DirectoryOptions extends Option<List<File>, DirectoryOptions> {
+
+ private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
+
+ public DirectoryOptions(final String description) {
+ super(description, ParameterCount.MULTI);
+ }
+
+ @Override
+ public String describeFormat() {
+ return existenceType.description + " directories";
+ }
+
+ @Override
+ public List<File> getValue() {
+ return parameters.stream().map(File::new).collect(Collectors.toList());
+ }
+
+ public DirectoryOptions mustExist() {
+ existenceType = ExistenceType.MUST_EXIST;
+ return this;
+ }
+
+ public DirectoryOptions mustNotExist() {
+ existenceType = ExistenceType.MUST_NOT_EXIST;
+ return this;
+ }
+
+ @Override
+ public boolean isValid(final String value) {
+ final File file = new File(value);
+
+ if (existenceType == ExistenceType.MUST_EXIST) {
+ return file.exists() && file.isDirectory();
+ }
+
+ if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+ return !file.exists();
+ }
+
+ if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isFile())
+ return false;
+
+ return true;
+ }
+
+ return false;
+ }
+
+}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-import java.io.File;
-
-public class DirectoryParameter extends Parameter<File, DirectoryParameter> {
-
- private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
-
- public DirectoryParameter(final String description) {
- super(description, ArgumentCount.SINGLE);
- }
-
- @Override
- public java.lang.String describeFormat() {
- return existenceType.description + " directory";
- }
-
- @Override
- public File getValue() {
-
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
-
- return new File(arguments.get(0));
- }
-
- public DirectoryParameter mustExist() {
- existenceType = ExistenceType.MUST_EXIST;
- return this;
- }
-
- public DirectoryParameter mustNotExist() {
- existenceType = ExistenceType.MUST_NOT_EXIST;
- return this;
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file = new File(value);
-
- if (existenceType == ExistenceType.MUST_EXIST) {
- return file.exists() && file.isDirectory();
- }
-
- if (existenceType == ExistenceType.MUST_NOT_EXIST) {
- return !file.exists();
- }
-
- if (existenceType == ExistenceType.DOES_NOT_MATTER) {
- if (file.exists())
- if (file.isFile())
- return false;
-
- return true;
- }
-
- return false;
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-import java.io.File;
-import java.util.List;
-import java.util.stream.Collectors;
-
-public class DirectoryParameters extends Parameter<List<File>, DirectoryParameters> {
-
- private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
-
- public DirectoryParameters(final String description) {
- super(description, ArgumentCount.MULTI);
- }
-
- @Override
- public String describeFormat() {
- return existenceType.description + " directories";
- }
-
- @Override
- public List<File> getValue() {
- return arguments.stream().map(File::new).collect(Collectors.toList());
- }
-
- public DirectoryParameters mustExist() {
- existenceType = ExistenceType.MUST_EXIST;
- return this;
- }
-
- public DirectoryParameters mustNotExist() {
- existenceType = ExistenceType.MUST_NOT_EXIST;
- return this;
- }
-
- @Override
- public boolean validate(final String value) {
- final File file = new File(value);
-
- if (existenceType == ExistenceType.MUST_EXIST) {
- return file.exists() && file.isDirectory();
- }
-
- if (existenceType == ExistenceType.MUST_NOT_EXIST) {
- return !file.exists();
- }
-
- if (existenceType == ExistenceType.DOES_NOT_MATTER) {
- if (file.exists())
- if (file.isFile())
- return false;
-
- return true;
- }
-
- return false;
- }
-
-}
*/
package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+/**
+ * This enum is used to define if resource denoted by particular option parameter shall exist or not.
+ * <p>
+ * This allows to specify for example if directory shall exist or not.
+ */
public enum ExistenceType {
- MUST_EXIST("existing"), MUST_NOT_EXIST("not existing"), DOES_NOT_MATTER("");
+ /**
+ * Resource shall exist.
+ */
+ MUST_EXIST("existing"),
+
+ /**
+ * Resource shall not exist.
+ */
+ MUST_NOT_EXIST("not existing"),
+
+ /**
+ * Resource existence does not matter.
+ */
+ DOES_NOT_MATTER("");
+ /**
+ * Human readable description of existence type.
+ */
public final String description;
ExistenceType(final String description) {
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+import java.io.File;
+
+public class FileOption extends Option<File, FileOption> {
+
+ private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
+
+ public FileOption(final String description) {
+ super(description, ParameterCount.SINGLE);
+ }
+
+ protected static boolean validateFile(ExistenceType existenceType, String value) {
+ final File file = new File(value);
+
+ if (existenceType == ExistenceType.MUST_EXIST) {
+ return file.exists() && file.isFile();
+ }
+
+ if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+ return !file.exists();
+ }
+
+ if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isDirectory())
+ return false;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return existenceType.description + " file";
+ }
+
+ @Override
+ public File getValue() {
+
+ if (parameters.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+
+ return new File(parameters.get(0));
+ }
+
+ public FileOption mustExist() {
+ existenceType = ExistenceType.MUST_EXIST;
+ return this;
+ }
+
+ public FileOption mustNotExist() {
+ existenceType = ExistenceType.MUST_NOT_EXIST;
+ return this;
+ }
+
+ @Override
+ public boolean isValid(final java.lang.String value) {
+ return validateFile(existenceType, value);
+ }
+
+}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+import java.io.File;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileOption.validateFile;
+
+public class FileOptions extends Option<List<File>, FileOptions> {
+
+ private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
+
+ public FileOptions(final String description) {
+ super(description, ParameterCount.MULTI);
+ }
+
+ @Override
+ public String describeFormat() {
+ return existenceType.description + " files";
+ }
+
+ @Override
+ public List<File> getValue() {
+ return parameters.stream().map(File::new).collect(Collectors.toList());
+ }
+
+ public FileOptions mustExist() {
+ existenceType = ExistenceType.MUST_EXIST;
+ return this;
+ }
+
+ public FileOptions mustNotExist() {
+ existenceType = ExistenceType.MUST_NOT_EXIST;
+ return this;
+ }
+
+ @Override
+ public boolean isValid(final String value) {
+ return validateFile(existenceType, value);
+ }
+
+}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-import java.io.File;
-
-public class FileParameter extends Parameter<File, FileParameter> {
-
- private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
-
- public FileParameter(final String description) {
- super(description, ArgumentCount.SINGLE);
- }
-
- protected static boolean validateFile(ExistenceType existenceType, String value) {
- final File file = new File(value);
-
- if (existenceType == ExistenceType.MUST_EXIST) {
- return file.exists() && file.isFile();
- }
-
- if (existenceType == ExistenceType.MUST_NOT_EXIST) {
- return !file.exists();
- }
-
- if (existenceType == ExistenceType.DOES_NOT_MATTER) {
- if (file.exists())
- if (file.isDirectory())
- return false;
-
- return true;
- }
-
- return false;
- }
-
- @Override
- public java.lang.String describeFormat() {
- return existenceType.description + " file";
- }
-
- @Override
- public File getValue() {
-
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
-
- return new File(arguments.get(0));
- }
-
- public FileParameter mustExist() {
- existenceType = ExistenceType.MUST_EXIST;
- return this;
- }
-
- public FileParameter mustNotExist() {
- existenceType = ExistenceType.MUST_NOT_EXIST;
- return this;
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- return validateFile(existenceType, value);
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-import java.io.File;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import static eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileParameter.validateFile;
-
-public class FileParameters extends Parameter<List<File>, FileParameters> {
-
- private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
-
- public FileParameters(final String description) {
- super(description, ArgumentCount.MULTI);
- }
-
- @Override
- public String describeFormat() {
- return existenceType.description + " files";
- }
-
- @Override
- public List<File> getValue() {
- return arguments.stream().map(File::new).collect(Collectors.toList());
- }
-
- public FileParameters mustExist() {
- existenceType = ExistenceType.MUST_EXIST;
- return this;
- }
-
- public FileParameters mustNotExist() {
- existenceType = ExistenceType.MUST_NOT_EXIST;
- return this;
- }
-
- @Override
- public boolean validate(final String value) {
- return validateFile(existenceType, value);
- }
-
-}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+public class IntegerOption extends Option<Integer, IntegerOption> {
+
+ public IntegerOption(final String description) {
+ super(description, ParameterCount.SINGLE);
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "integer";
+ }
+
+ @Override
+ public Integer getValue() {
+ if (parameters.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+ return Integer.parseInt(parameters.get(0));
+ }
+
+ @Override
+ public boolean isValid(final java.lang.String value) {
+ try {
+ java.lang.Integer.valueOf(value);
+ return true;
+ } catch (final NumberFormatException e) {
+ return false;
+ }
+ }
+}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-public class IntegerParameter extends Parameter<Integer, IntegerParameter> {
-
- public IntegerParameter(final String description) {
- super(description, ArgumentCount.SINGLE);
- }
-
- @Override
- public java.lang.String describeFormat() {
- return "integer";
- }
-
- @Override
- public Integer getValue() {
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
- return Integer.parseInt(arguments.get(0));
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- try {
- java.lang.Integer.valueOf(value);
- return true;
- } catch (final NumberFormatException e) {
- return false;
- }
- }
-}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+public class NullOption extends Option<Boolean, NullOption> {
+
+ public NullOption(final String description) {
+ super(description, ParameterCount.NONE);
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "<none>";
+ }
+
+ @Override
+ public Boolean getValue() {
+ return isPresent();
+ }
+
+ @Override
+ public boolean isValid(final java.lang.String value) {
+ return true;
+ }
+
+}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-public class NullParameter extends Parameter<Boolean, NullParameter> {
-
- public NullParameter(final String description) {
- super(description, ArgumentCount.NONE);
- }
-
- @Override
- public java.lang.String describeFormat() {
- return "<none>";
- }
-
- @Override
- public Boolean getValue() {
- return isSpecified();
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- return true;
- }
-
-}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+public class StringOption extends Option<String, StringOption> {
+
+ public final String defaultValue;
+
+ public StringOption(final String description) {
+ super(description, ParameterCount.SINGLE);
+ defaultValue = null;
+ }
+ public StringOption(final String description, String defaultValue) {
+ super(description, ParameterCount.SINGLE);
+ this.defaultValue = defaultValue;
+ this.setPresent(true);
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "string";
+ }
+
+ @Override
+ public String getValue() {
+
+ if (parameters.isEmpty() && (defaultValue != null)) return defaultValue;
+
+ if (parameters.size() == 1) return parameters.get(0);
+
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+ }
+
+ @Override
+ public boolean isValid(final java.lang.String value) {
+ return true;
+ }
+
+}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
+
+import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
+
+import java.util.List;
+
+public class StringOptions extends Option<List<String>, StringOptions> {
+
+ public StringOptions(final String description) {
+ super(description, ParameterCount.MULTI);
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "one to many strings";
+ }
+
+ @Override
+ public List<String> getValue() {
+ return parameters;
+ }
+
+ @Override
+ public boolean isValid(final java.lang.String value) {
+ return true;
+ }
+
+}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-public class StringParameter extends Parameter<String, StringParameter> {
-
- public final String defaultValue;
-
- public StringParameter(final String description) {
- super(description, ArgumentCount.SINGLE);
- defaultValue = null;
- }
- public StringParameter(final String description, String defaultValue) {
- super(description, ArgumentCount.SINGLE);
- this.defaultValue = defaultValue;
- this.setSpecified(true);
- }
-
- @Override
- public java.lang.String describeFormat() {
- return "string";
- }
-
- @Override
- public String getValue() {
-
- if (arguments.isEmpty() && (defaultValue != null)) return defaultValue;
-
- if (arguments.size() == 1) return arguments.get(0);
-
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- return true;
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
-
-import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
-
-import java.util.List;
-
-public class StringParameters extends Parameter<List<String>, StringParameters> {
-
- public StringParameters(final String description) {
- super(description, ArgumentCount.MULTI);
- }
-
- @Override
- public java.lang.String describeFormat() {
- return "one to many strings";
- }
-
- @Override
- public List<String> getValue() {
- return arguments;
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- return true;
- }
-
-}
*/
package eu.svjatoslav.commons.cli_helper.parameter_parser;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileParameter;
-import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.StringParameter;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileOption;
+import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.StringOption;
import org.junit.Before;
import org.junit.Test;
public void testParse() throws IOException {
// define allowed parameters
- final StringParameter helpParameter = parser.add(new StringParameter("Show help screen")
+ final StringOption helpOption = parser.add(new StringOption("Show help screen")
.addAliases("--help", "-h").setMandatory());
- final StringParameter compileParameter = parser.add(new StringParameter("Compile code"))
+ final StringOption compileOption = parser.add(new StringOption("Compile code"))
.addAliases("--compile", "-c");
- FileParameter fileParameter = parser.add(new FileParameter("Input file")
+ FileOption fileOption = parser.add(new FileOption("Input file")
.addAliases("-i").mustExist());
createTemporaryFile();
parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"});
// --help was in the arguments
- assertTrue(helpParameter.isSpecified());
+ assertTrue(helpOption.isPresent());
// compile was not present
- assertFalse(compileParameter.isSpecified());
+ assertFalse(compileOption.isPresent());
// validate that help argument was "section"
- assertEquals("section", helpParameter.getValue());
+ assertEquals("section", helpOption.getValue());
- assertTrue(fileParameter.isSpecified());
- assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath());
+ assertTrue(fileOption.isPresent());
+ assertEquals("/tmp/file with spaces", fileOption.getValue().getAbsolutePath());
}