Files
cligen/docs/CliGen/Argument.html

576 lines
21 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Crystal Docs 1.20.3">
<meta name="crystal_docs.project_version" content="object_rework-dev">
<meta name="crystal_docs.project_name" content="CliGenerator">
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/doc.js"></script>
<meta name="repository-name" content="CliGenerator">
<title>CliGen::Argument - CliGenerator object_rework-dev</title>
<script type="text/javascript">
CrystalDocs.base_path = "../";
</script>
</head>
<body>
<svg class="hidden">
<symbol id="octicon-link" viewBox="0 0 16 16">
<path fill="currentColor" fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
</symbol>
</svg>
<input type="checkbox" id="sidebar-btn">
<label for="sidebar-btn" id="sidebar-btn-label">
<svg class="open" xmlns="http://www.w3.org/2000/svg" height="2em" width="2em" viewBox="0 0 512 512"><title>Open Sidebar</title><path fill="currentColor" d="M80 96v64h352V96H80zm0 112v64h352v-64H80zm0 112v64h352v-64H80z"></path></svg>
<svg class="close" xmlns="http://www.w3.org/2000/svg" width="2em" height="2em" viewBox="0 0 512 512"><title>Close Sidebar</title><path fill="currentColor" d="m118.6 73.4-45.2 45.2L210.7 256 73.4 393.4l45.2 45.2L256 301.3l137.4 137.3 45.2-45.2L301.3 256l137.3-137.4-45.2-45.2L256 210.7Z"></path></svg>
</label>
<div class="sidebar">
<div class="sidebar-header">
<div class="search-box">
<input type="search" class="search-input" placeholder="Search..." spellcheck="false" aria-label="Search">
</div>
<div class="project-summary">
<h1 class="project-name">
<a href="../index.html">
CliGenerator
</a>
</h1>
<span class="project-version">
object_rework-dev
</span>
</div>
</div>
<div class="search-results hidden">
<ul class="search-list"></ul>
</div>
<div class="types-list">
<ul>
<li class="parent open current" data-id="CliGenerator/CliGen" data-name="cligen">
<a href="../CliGen.html">CliGen</a>
<ul>
<li class=" " data-id="CliGenerator/CliGen/App" data-name="cligen::app">
<a href="../CliGen/App.html">App</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Arg" data-name="cligen::arg">
<a href="../CliGen/Arg.html">Arg</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/ArgReprocessedError" data-name="cligen::argreprocessederror">
<a href="../CliGen/ArgReprocessedError.html">ArgReprocessedError</a>
</li>
<li class=" current" data-id="CliGenerator/CliGen/Argument" data-name="cligen::argument">
<a href="../CliGen/Argument.html">Argument</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/BaseCommandNode" data-name="cligen::basecommandnode">
<a href="../CliGen/BaseCommandNode.html">BaseCommandNode</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/BaseFlag" data-name="cligen::baseflag">
<a href="../CliGen/BaseFlag.html">BaseFlag</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Coercable" data-name="cligen::coercable">
<a href="../CliGen/Coercable.html">Coercable</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Command" data-name="cligen::command">
<a href="../CliGen/Command.html">Command</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/CommandInfo" data-name="cligen::commandinfo">
<a href="../CliGen/CommandInfo.html">CommandInfo</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/CommandMeta" data-name="cligen::commandmeta">
<a href="../CliGen/CommandMeta.html">CommandMeta</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/CommandNode" data-name="cligen::commandnode(t)">
<a href="../CliGen/CommandNode.html">CommandNode</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Common" data-name="cligen::common">
<a href="../CliGen/Common.html">Common</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/ConfigurationError" data-name="cligen::configurationerror">
<a href="../CliGen/ConfigurationError.html">ConfigurationError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/DuplicateCommandError" data-name="cligen::duplicatecommanderror">
<a href="../CliGen/DuplicateCommandError.html">DuplicateCommandError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/DuplicateFlagError" data-name="cligen::duplicateflagerror">
<a href="../CliGen/DuplicateFlagError.html">DuplicateFlagError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Error" data-name="cligen::error">
<a href="../CliGen/Error.html">Error</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Flag" data-name="cligen::flag(t)">
<a href="../CliGen/Flag.html">Flag</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/FlagArgumentError" data-name="cligen::flagargumenterror">
<a href="../CliGen/FlagArgumentError.html">FlagArgumentError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/FlagBundleError" data-name="cligen::flagbundleerror">
<a href="../CliGen/FlagBundleError.html">FlagBundleError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/FlagMeta" data-name="cligen::flagmeta">
<a href="../CliGen/FlagMeta.html">FlagMeta</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/FlagMissingArgumentError" data-name="cligen::flagmissingargumenterror">
<a href="../CliGen/FlagMissingArgumentError.html">FlagMissingArgumentError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/FlagNotFoundError" data-name="cligen::flagnotfounderror">
<a href="../CliGen/FlagNotFoundError.html">FlagNotFoundError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Format" data-name="cligen::format">
<a href="../CliGen/Format.html">Format</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/HelpRequestedError" data-name="cligen::helprequestederror">
<a href="../CliGen/HelpRequestedError.html">HelpRequestedError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/InternalError" data-name="cligen::internalerror">
<a href="../CliGen/InternalError.html">InternalError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/InternalVar" data-name="cligen::internalvar">
<a href="../CliGen/InternalVar.html">InternalVar</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/InvalidFlagValueError" data-name="cligen::invalidflagvalueerror">
<a href="../CliGen/InvalidFlagValueError.html">InvalidFlagValueError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/InvalidOptionError" data-name="cligen::invalidoptionerror">
<a href="../CliGen/InvalidOptionError.html">InvalidOptionError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/MatchType" data-name="cligen::matchtype">
<a href="../CliGen/MatchType.html">MatchType</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/MissingDispatchError" data-name="cligen::missingdispatcherror">
<a href="../CliGen/MissingDispatchError.html">MissingDispatchError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/MissingRequiredFlagError" data-name="cligen::missingrequiredflagerror">
<a href="../CliGen/MissingRequiredFlagError.html">MissingRequiredFlagError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Parsable" data-name="cligen::parsable">
<a href="../CliGen/Parsable.html">Parsable</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/ParseableInvariantError" data-name="cligen::parseableinvarianterror">
<a href="../CliGen/ParseableInvariantError.html">ParseableInvariantError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/PreRunCommand" data-name="cligen::preruncommand">
<a href="../CliGen/PreRunCommand.html">PreRunCommand</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/ProxyCommand" data-name="cligen::proxycommand">
<a href="../CliGen/ProxyCommand.html">ProxyCommand</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Regex" data-name="cligen::regex">
<a href="../CliGen/Regex.html">Regex</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/RegexInvariantError" data-name="cligen::regexinvarianterror">
<a href="../CliGen/RegexInvariantError.html">RegexInvariantError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/ReservedFlagError" data-name="cligen::reservedflagerror">
<a href="../CliGen/ReservedFlagError.html">ReservedFlagError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/RunCommand" data-name="cligen::runcommand">
<a href="../CliGen/RunCommand.html">RunCommand</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/RuntimeError" data-name="cligen::runtimeerror">
<a href="../CliGen/RuntimeError.html">RuntimeError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/SubCommand" data-name="cligen::subcommand">
<a href="../CliGen/SubCommand.html">SubCommand</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/SubCommandInfo" data-name="cligen::subcommandinfo">
<a href="../CliGen/SubCommandInfo.html">SubCommandInfo</a>
</li>
<li class="parent " data-id="CliGenerator/CliGen/Timeparse" data-name="cligen::timeparse">
<a href="../CliGen/Timeparse.html">Timeparse</a>
<ul>
<li class=" " data-id="CliGenerator/CliGen/Timeparse/OperationUnit" data-name="cligen::timeparse::operationunit">
<a href="../CliGen/Timeparse/OperationUnit.html">OperationUnit</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Timeparse/RelativeOperation" data-name="cligen::timeparse::relativeoperation">
<a href="../CliGen/Timeparse/RelativeOperation.html">RelativeOperation</a>
</li>
</ul>
</li>
<li class=" " data-id="CliGenerator/CliGen/TimeParseError" data-name="cligen::timeparseerror">
<a href="../CliGen/TimeParseError.html">TimeParseError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/Trigger" data-name="cligen::trigger">
<a href="../CliGen/Trigger.html">Trigger</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/UnknownCommandNodeError" data-name="cligen::unknowncommandnodeerror">
<a href="../CliGen/UnknownCommandNodeError.html">UnknownCommandNodeError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/UnknownFlagError" data-name="cligen::unknownflagerror">
<a href="../CliGen/UnknownFlagError.html">UnknownFlagError</a>
</li>
<li class=" " data-id="CliGenerator/CliGen/ValidationError" data-name="cligen::validationerror">
<a href="../CliGen/ValidationError.html">ValidationError</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="main-content">
<h1 class="type-name">
<span class="kind">
annotation
</span> CliGen::<wbr>Argument
</h1>
<h2>
<a id="overview" class="anchor" href="#overview">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>
Overview
</h2>
<p>This is used for annotating instance variables for the CliGen framework can know how to create your <code><a href="../CliGen/Flag.html">CliGen::Flag</a>(T)</code> objects</p>
<p>WHILE this is usually being handled by the <code><a href="../CliGen/Command.html#argument%28variable%2Cdescription%2Clong%3Dnil%2Cshort%3Dnil%2Cvalidation%3Dnil%2Con_match%3Dnil%2Cdef_setter%3Dfalse%2Cdef_getter%3Dfalse%2Coptions%3Dnil%2Cdelimiter%3D%22%2C%22%2Cformat%3Dnil%2Callow_no_verification%3Dfalse%2Cenv_var%3D%22%22%29-macro">CliGen::Command.argument</a></code> macro
inside of the class body.</p>
<p>EX:</p>
<pre><code class="language-crystal"><span class="k">class</span> <span class="t">MyCmd</span> <span class="o">&lt;</span> <span class="t">CliGen</span><span class="t">::</span><span class="t">Command</span>
argument(myvar : <span class="t">String</span> <span class="o">=</span> <span class="s">&quot;test&quot;</span>,
short: <span class="s">&quot;-m&quot;</span>,
long: <span class="s">&quot;--myvar&quot;</span>,
description: <span class="s">&quot;This is my test flag&quot;</span>,
options: <span class="s">%w[ test test2 test3 ]</span>
)
<span class="k">def</span> <span class="m">main</span>
puts <span class="s">&quot;@myvar was </span><span class="i">#{</span>@myvar<span class="i">}</span><span class="s">&quot;</span>
<span class="k">end</span>
<span class="k">end</span></code></pre>
<p>However, this can also be done manually if you don't want to use the macros
you will make me sad, but otherwise it's understandable if you want do it
manually. Just understand that the macros are there for doing all of the
validations for user-friendly implementation.</p>
<h2><a id="expected-metadata" class="anchor" href="#expected-metadata">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>Expected Metadata:</h2>
<h3><a id="short" class="anchor" href="#short">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>short:</h3>
<p>Type: StringLiteral</p>
<p>Required: false</p>
<p>This represents the short form of the flag bring provided. it is optional as
not all flags have to have a short form flag.</p>
<h3><a id="long" class="anchor" href="#long">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>long:</h3>
<p>Type: StringLiteral</p>
<p>Required: true</p>
<p>This represents the long-form of the flag. It is required in order to generate
the <code><a href="../CliGen/Flag.html">Flag</a>(T)</code>.</p>
<h3><a id="description" class="anchor" href="#description">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>description:</h3>
<p>Type: StringLiteral
Required: true</p>
<p>This is the description of your flag and is required for <code><a href="../CliGen/Flag.html">Flag</a>(T)</code> creation</p>
<h3><a id="delimiter" class="anchor" href="#delimiter">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>delimiter:</h3>
<p>Type: StringLiteral</p>
<p>Required: false</p>
<p>For <code><a href="../CliGen/Flag.html">Flag</a></code>(Array(T)) flags this is the delimiter that will seperate any inline
args (ex: &quot;,&quot; will split &quot;a,b,c&quot;) provided at the commandline. If nil/not
provided, the framework will default to ',' as this is the usual choice.</p>
<h3><a id="env-var" class="anchor" href="#env-var">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>env_var:</h3>
<p>Type: StringLiteral</p>
<p>Required: false</p>
<p>This is the ENV VAR that can be used to specify your flag value when not
provided by the user.</p>
<h3><a id="validation" class="anchor" href="#validation">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>validation:</h3>
<p>Type: ProcLiteral</p>
<p>Required: false</p>
<p>This is a proc that can be used to provide an ad-hoc way of verifying the
value provided by a user.</p>
<p>EX: Int Validator</p>
<pre><code class="language-crystal">validation: <span class="o">-&gt;</span>(i : <span class="t">Int32</span>) : <span class="t">Bool</span> <span class="k">do</span>
(<span class="n">1</span>..<span class="n">23</span>).includes?(i)
<span class="k">end</span></code></pre>
<p>This is used as a fallback to where the options: key doesn't cleanly
provide enough of a check for the provided values.</p>
<p>Note:
The input value MUST be the same as the value type as the instance
variable. Otherwise CliGen will not compile. IF requested I can
add a raw_validation: key as well to do the same but for just the
String variable provided by the user.</p>
<h3><a id="on-match" class="anchor" href="#on-match">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>on_match:</h3>
<p>Type: ProcLiteral</p>
<p>Required: false</p>
<p>Much like validation, this is used as a hook for doing arbitrary actions
with the parsed value from the user (very useful for global flags).</p>
<p>EX: Log level setter</p>
<pre><code class="language-crystal">on_match: <span class="o">-&gt;</span>(arg : <span class="t">String</span>) <span class="k">do</span>
<span class="k">begin</span>
<span class="t">::</span><span class="t">Log</span>.setup(level: <span class="t">::</span><span class="t">Log</span><span class="t">::</span><span class="t">Severity</span>.parse(arg))
<span class="k">rescue</span> e : <span class="t">ArgumentError</span>
<span class="t">STDERR</span>.puts <span class="s">&quot;ERROR : Failed to set to </span><span class="i">#{</span>arg<span class="i">}</span><span class="s"> log level: (</span><span class="i">#{</span>e.<span class="k">class</span><span class="i">}</span><span class="s">: </span><span class="i">#{</span>e.message<span class="i">}</span><span class="s">)&quot;</span>
<span class="k">end</span>
<span class="k">end</span></code></pre>
<p>In this way you can use on_match: to hook a global flag and have it call some
arbitrary method elsewhere in the codebase to help setup the environment
before the main command is run.</p>
<h3><a id="options" class="anchor" href="#options">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>options:</h3>
<p>Type: ArrayLiteral(T)|Call</p>
<p>Required: false</p>
<p>CURRENTLY this is being as a way of providing a static set of values that we
are to use when doing a provided argument.</p>
<p>EX: Options for string var</p>
<pre><code class="language-crystal">options: <span class="s">%w[ a b c ]</span></code></pre>
<p>Howver, this currently also
supports delegating the retrieval of values (in array format) to be learned
at runtime by providing a call to a global methods/class method/util
method/etc</p>
<p>EX: Deletgating to runtime</p>
<pre><code class="language-crystal"><span class="k">module</span> <span class="t">MyModule</span>
<span class="k">def</span> <span class="m">self</span>.my_method : <span class="t">Array</span>(<span class="t">String</span>)
<span class="k">if</span> <span class="t">File</span>.exists?(<span class="s">&quot;/etc/valid_things.txt&quot;</span>)
<span class="t">File</span>.read(<span class="s">&quot;/etc/valid_things.txt&quot;</span>).split(<span class="s">&quot;,&quot;</span>)
<span class="k">else</span>
<span class="s">%w[ a b c ]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="t">CliGen</span>.add_global_flag(<span class="t">String</span>,
short: <span class="s">&quot;-t&quot;</span>,
long: <span class="s">&quot;--test&quot;</span>,
description: <span class="s">&quot;This does things. I promise&quot;</span>,
options: <span class="t">::</span><span class="t">MyModule</span>.my_method,
on_match: <span class="o">-&gt;</span>(t : <span class="t">String</span>) <span class="k">do</span>
puts <span class="s">&quot;Matched </span><span class="i">#{</span>t<span class="i">}</span><span class="s">&quot;</span>
<span class="k">end</span>
)
<span class="k">end</span></code></pre>
<p>Doing things this way gives you some runtime flexibility, but makes you
responsible for ensuring that it doesn't crash or provide incorrect data
at runtime. As (unfortunately) the framework doesn't account for developer
error at runtime like it can at compile-time with a static array of
values.</p>
<h3><a id="format" class="anchor" href="#format">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>format:</h3>
<p>Type: RegexLiteral</p>
<p>Required: false</p>
<p>This metadata is used to provide (mostly for strings when you don't have a
statically known list of values that can be provided at runtime, but you
want to filter out invalid options.</p>
<p>EX: filtering for csv formatted info</p>
<pre><code class="language-crystal">format: <span class="s">/^([a-z0-9]+)(,?[a-z0-9]+)+$/</span></code></pre>
<h2>
<a id="defined-in" class="anchor" href="#defined-in">
<svg class="octicon-link" aria-hidden="true">
<use href="#octicon-link"/>
</svg>
</a>
Defined in:
</h2>
cligen/annotations.cr
<br/>
<div class="methods-inherited">
</div>
</div>
</body>
</html>