cmdr
Artifact [f1111cd8e5]
Not logged in
Bounty program for improvements to Tcl and certain Tcl packages.

Artifact f1111cd8e5ec7585a58389e6b2a9832e0c8b9639:



<html><head>
<title>cmdr-user-vtypes - Cmdr, a framework for command line parsing and dispatch</title>
<style type="text/css"><!--
    HTML {
	background: 	#FFFFFF;
	color: 		black;
    }
    BODY {
	background: 	#FFFFFF;
	color:	 	black;
    }
    DIV.doctools {
	margin-left:	10%;
	margin-right:	10%;
    }
    DIV.doctools H1,DIV.doctools H2 {
	margin-left:	-5%;
    }
    H1, H2, H3, H4 {
	margin-top: 	1em;
	font-family:	sans-serif;
	font-size:	large;
	color:		#005A9C;
	background: 	transparent;
	text-align:		left;
    }
    H1.title {
	text-align: center;
    }
    UL,OL {
	margin-right: 0em;
	margin-top: 3pt;
	margin-bottom: 3pt;
    }
    UL LI {
	list-style: disc;
    }
    OL LI {
	list-style: decimal;
    }
    DT {
	padding-top: 	1ex;
    }
    UL.toc,UL.toc UL, UL.toc UL UL {
	font:		normal 12pt/14pt sans-serif;
	list-style:	none;
    }
    LI.section, LI.subsection {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding: 	0em;
    }
    PRE {
	display: 	block;
	font-family:	monospace;
	white-space:	pre;
	margin:		0%;
	padding-top:	0.5ex;
	padding-bottom:	0.5ex;
	padding-left:	1ex;
	padding-right:	1ex;
	width:		100%;
    }
    PRE.example {
	color: 		black;
	background: 	#f5dcb3;
	border:		1px solid black;
    }
    UL.requirements LI, UL.syntax LI {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding:	0em;
    }
    DIV.synopsis {
	color: 		black;
	background: 	#80ffff;
	border:		1px solid black;
	font-family:	serif;
	margin-top: 	1em;
	margin-bottom: 	1em;
    }
    UL.syntax {
	margin-top: 	1em;
	border-top:	1px solid black;
    }
    UL.requirements {
	margin-bottom: 	1em;
	border-bottom:	1px solid black;
    }
--></style>
</head>
<! -- Generated from file 'cmdr_vtypes.man' by tcllib/doctools with format 'html'
   -->
<! -- Copyright &copy; 2013-2016 Andreas Kupries   -- Copyright &copy; 2013-2016 Documentation, Andreas Kupries
   -->
<! -- CVS: $Id$ cmdr-user-vtypes.n
   -->
<body><div class="doctools">
<hr> [
   <a href="../../../../../../home">Home</a>
| <a href="../../toc.html">Main Table Of Contents</a>
| <a href="../toc.html">Table Of Contents</a>
| <a href="../../index.html">Keyword Index</a>
 ] <hr>
<h1 class="title">cmdr-user-vtypes(n) 1 doc &quot;Cmdr, a framework for command line parsing and dispatch&quot;</h1>
<div id="name" class="section"><h2><a name="name">Name</a></h2>
<p>cmdr-user-vtypes - Cmdr - Writing custom validation types</p>
</div>
<div id="toc" class="section"><h2><a name="toc">Table Of Contents</a></h2>
<ul class="toc">
<li class="section"><a href="#toc">Table Of Contents</a></li>
<li class="section"><a href="#synopsis">Synopsis</a></li>
<li class="section"><a href="#section1">Description</a></li>
<li class="section"><a href="#section2">Background</a></li>
<li class="section"><a href="#section3">API</a></li>
<li class="section"><a href="#section4">Example</a></li>
<li class="section"><a href="#section5">Bugs, Ideas, Feedback</a></li>
<li class="section"><a href="#keywords">Keywords</a></li>
<li class="section"><a href="#copyright">Copyright</a></li>
</ul>
</div>
<div id="synopsis" class="section"><h2><a name="synopsis">Synopsis</a></h2>
<div class="synopsis">
<ul class="requirements">
<li>package require <b class="pkgname">cmdr::validate</b></li>
</ul>
<ul class="syntax">
<li><a href="#1"><b class="cmd">&lt;v-type&gt;</b> <b class="method">complete</b> <i class="arg">p</i> <i class="arg">x</i></a></li>
<li><a href="#2"><b class="cmd">&lt;v-type&gt;</b> <b class="method">default</b> <i class="arg">p</i></a></li>
<li><a href="#3"><b class="cmd">&lt;v-type&gt;</b> <b class="method">release</b> <i class="arg">p</i> <i class="arg">x</i></a></li>
<li><a href="#4"><b class="cmd">&lt;v-type&gt;</b> <b class="method">validate</b> <i class="arg">p</i> <i class="arg">x</i></a></li>
</ul>
</div>
</div>
<div id="section1" class="section"><h2><a name="section1">Description</a></h2>
<p>Welcome to the Cmdr project, written by Andreas Kupries.</p>
<p>For availability please read <i class="term"><a href="cmdr_howto_get_sources.html">Cmdr - How To Get The Sources</a></i>.</p>
<p>This document describes the API expected of <i class="term">validation types</i>
to make them usable within the Cmdr framework, and how to
write a custom validation type.</p>
<p>Readers interested in the standard validation types of the
framework should read <i class="term"><a href="cmdr_validate.html">Cmdr - Standard validation types for parameters</a></i>.</p>
</div>
<div id="section2" class="section"><h2><a name="section2">Background</a></h2>
<p>Validation types are Cmdr's answer to the necessity of moving
between the string and internal representations of
<b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> instances.
Given a <i class="term">string representation</i> they either return the
associated <i class="term">internal representation</i> or raise an error,
signaling that the string was illegal. This part of their work, the
verification of the legality of the input string gave them their name.</p>
<p>The general concept of <i class="term">validation types</i> was taken from
<b class="package">snit</b>, and modified to suit Cmdr.
Where snit's types expect only a single method to validate the input
Cmdr expects all types to support an ensemble of <em>four</em>
methods.
One for the basic validation and transformation of the input, another
for the release of any internal representation so generated, plus two
more for delivery of a default representation and support for command
line completion.
The details (method names, signatures, etc.) can be found in section
<span class="sectref"><a href="#section3">API</a></span> below.</p>
<p>As an example the implementation of the standard boolean
validation type is shown in section <span class="sectref"><a href="#section4">Example</a></span>.</p>
<p>It should be noted that while <b class="package">snit</b>'s validation types
in principle allow for the transformation of input into a disparate
internal representation, they never went so far as to allow complex
representations which might require the release of resources after
use.</p>
<p>The <b class="cmd">validate</b> and <b class="cmd">release</b> methods are primarily used
during either <i class="term">Completion</i> or <i class="term">Execution</i> phases, depending
on the chosen deferal state.
They may also be used during the <i class="term">Parsing</i> phase, for optional
<i class="term">inputs</i> under the <b class="cmd">test</b>-regime].</p>
<p>The <b class="cmd">complete</b> method will be used whereever the system
activates an interactive command line shell where arguments may be
assigned to parameters.</p>
<p>The <b class="cmd">default</b> method on the other hand can expect to be
invoked during the <i class="term">Dispatch</i> phase, as part of the system's
declaration processing, if not preempted by <b class="cmd">default</b> and
<b class="cmd">generate</b> declarations for the parameter.
Note here that the <b class="cmd">default</b> method has the same signature as a
paramete's <b class="cmd">generate</b> callback and can be used as such.
This is actually needed and useful when the default internal
representation for a validation type cannot be expressed as a fixed
value and its creation while parsing the specification itself is too
early.
We can still use the validation type for its generation, by hooking it
explicitly into <b class="cmd">generate</b> to change the timing of its invokation.</p>
</div>
<div id="section3" class="section"><h2><a name="section3">API</a></h2>
<p>In the descriptions below the <b class="cmd">&lt;v-type&gt;</b> is a placeholder for the
actual command prefix, most often a main command, of the validation
type.</p>
<dl class="definitions">
<dt><a name="1"><b class="cmd">&lt;v-type&gt;</b> <b class="method">complete</b> <i class="arg">p</i> <i class="arg">x</i></a></dt>
<dd><p>This method is invoked during command completion done by the framework.</p>
<p>It has to return the list of legal string representations for
the type and parameter instance <i class="arg">p</i> which have the incomplete word
<i class="arg">x</i> as their prefix.</p>
<dl class="arguments">
<dt><b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> <i class="arg">p</i></dt>
<dd><p>The <b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> instance governing the completion
process.  While the standard validation types do not make use of it a
custom type may have need for access to the context of the completion.</p></dd>
<dt>string <i class="arg">x</i></dt>
<dd><p>The string value to complete.</p></dd>
</dl></dd>
<dt><a name="2"><b class="cmd">&lt;v-type&gt;</b> <b class="method">default</b> <i class="arg">p</i></a></dt>
<dd><p>This method is invoked when the framework has to determine the
internal representation of a parameter which has no user-specified
string representation.</p>
<p>It has to return the default internal representation for
the type and parameter instance <i class="arg">p</i>.</p>
<dl class="arguments">
<dt><b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> <i class="arg">p</i></dt>
<dd><p>The <b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> instance whose default internal
representation is to be computed. While the standard validation types
do not make use of it a custom type may have need for access to the
context.</p></dd>
</dl></dd>
<dt><a name="3"><b class="cmd">&lt;v-type&gt;</b> <b class="method">release</b> <i class="arg">p</i> <i class="arg">x</i></a></dt>
<dd><p>This method is invoked when the framework has to get rid of an
internal representation for a parameter.</p>
<p>It has to release any resources associated with the internal
representation <i class="arg">x</i> of parameter instance <i class="arg">p</i>.</p>
<p>Note that the result of this method, if there is any, is
ignored by the framework.</p>
<dl class="arguments">
<dt><b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> <i class="arg">p</i></dt>
<dd><p>The <b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> instance holding the internal
representation to release. While the standard validation types do not
make use of it a custom type may have need for access to the context
of the completion.</p></dd>
<dt>string <i class="arg">x</i></dt>
<dd><p>The internal representation to release.</p></dd>
</dl></dd>
<dt><a name="4"><b class="cmd">&lt;v-type&gt;</b> <b class="method">validate</b> <i class="arg">p</i> <i class="arg">x</i></a></dt>
<dd><p>This method is invoked during to validate and convert a string
representation.</p>
<p>It has to verify that <i class="arg">x</i> is a legal string representation
for the parameter instance <i class="arg">p</i>, and return the associated internal
representation.</p>
<dl class="arguments">
<dt><b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> <i class="arg">p</i></dt>
<dd><p>The <b class="package"><a href="cmdr_parameter.html">cmdr::parameter</a></b> instance governing the validation
process. The standard validation types make use of it in case of a
validation failure to generate a proper error message.
See also <i class="term">Utilities for Validation Types</i> for commands helping
with keeping validation error messages uniform.</p></dd>
<dt>string <i class="arg">x</i></dt>
<dd><p>The string value to validate cand convert.</p></dd>
</dl></dd>
</dl>
</div>
<div id="section4" class="section"><h2><a name="section4">Example</a></h2>
<p>As an example the implementation of the standard boolean validation
type is shown here.</p>
<p>Note that while this example uses a <b class="cmd">namespace ensemble</b>
other methods are possible too, i.e. all the various object systems
for Tcl would be suitable as well.</p>
<pre class="example">
package require cmdr::validate::common
namespace eval ::cmdr::validate::boolean {
    namespace export default validate complete release
    namespace ensemble create
    namespace import ::cmdr::validate::common::fail
    namespace import ::cmdr::validate::common::complete-enum
}
proc ::cmdr::validate::boolean::release {p x} {
    # Simple internal representation. Nothing to release.
    return
}
proc ::cmdr::validate::boolean::default {p}  {
    return no
}
proc ::cmdr::validate::boolean::complete {p x} {
    # x is string representation. Result as well.
    return [complete-enum {
	yes no false true on off 0 1
    } 1 $x]
}
proc ::cmdr::validate::boolean::validate {p x} {
    # x is string representation. Result is internal representation.
    if {[string is boolean -strict $x]} {
	return $x
    }
    fail $p BOOLEAN &quot;a boolean&quot; $x
}
</pre>
</div>
<div id="section5" class="section"><h2><a name="section5">Bugs, Ideas, Feedback</a></h2>
<p>Both the package(s) and this documentation will undoubtedly contain
bugs and other problems.
Please report such at
<a href="https:/core.tcl.tk/akupries/cmdr">Cmdr Tickets</a>.</p>
<p>Please also report any ideas you may have for enhancements of
either package(s) and/or documentation.</p>
</div>
<div id="keywords" class="section"><h2><a name="keywords">Keywords</a></h2>
<p><a href="../../index.html#key4">arguments</a>, <a href="../../index.html#key5">command hierarchy</a>, <a href="../../index.html#key9">command line completion</a>, <a href="../../index.html#key11">command line handling</a>, <a href="../../index.html#key13">command tree</a>, <a href="../../index.html#key0">editing command line</a>, <a href="../../index.html#key8">help for command line</a>, <a href="../../index.html#key6">hierarchy of commands</a>, <a href="../../index.html#key3">interactive command shell</a>, <a href="../../index.html#key1">optional arguments</a>, <a href="../../index.html#key2">options</a>, <a href="../../index.html#key12">parameters</a>, <a href="../../index.html#key10">processing command line</a>, <a href="../../index.html#key7">tree of commands</a></p>
</div>
<div id="copyright" class="section"><h2><a name="copyright">Copyright</a></h2>
<p>Copyright &copy; 2013-2016 Andreas Kupries<br>
Copyright &copy; 2013-2016 Documentation, Andreas Kupries</p>
</div>
</div></body></html>