File: Parsing-Arguments.html

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (197 lines) | stat: -rw-r--r-- 9,151 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Parsing Arguments (GNU Octave (version 6.2.0))</title>

<meta name="description" content="Parsing Arguments (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Parsing Arguments (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Validating-Arguments.html" rel="up" title="Validating Arguments">
<link href="Function-Files.html" rel="next" title="Function Files">
<link href="Validating-the-type-of-Arguments.html" rel="prev" title="Validating the type of Arguments">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<span id="Parsing-Arguments"></span><div class="header">
<p>
Previous: <a href="Validating-the-type-of-Arguments.html" accesskey="p" rel="prev">Validating the type of Arguments</a>, Up: <a href="Validating-Arguments.html" accesskey="u" rel="up">Validating Arguments</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Parsing-Arguments-1"></span><h4 class="subsection">11.9.3 Parsing Arguments</h4>

<p>If none of the preceding validation functions is sufficient there is also
the class <code>inputParser</code> which can perform extremely complex input
checking for functions.
</p>
<span id="XREFinputParser"></span><dl>
<dt id="index-inputParser">: <em><var>p</var> =</em> <strong>inputParser</strong> <em>()</em></dt>
<dd><p>Create object <var>p</var> of the inputParser class.
</p>
<p>This class is designed to allow easy parsing of function arguments.  The
class supports four types of arguments:
</p>
<ol>
<li> mandatory (see <code>addRequired</code>);

</li><li> optional (see <code>addOptional</code>);

</li><li> named (see <code>addParameter</code>);

</li><li> switch (see <code>addSwitch</code>).
</li></ol>

<p>After defining the function API with these methods, the supplied arguments
can be parsed with the <code>parse</code> method and the parsing results
accessed with the <code>Results</code> accessor.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eParameters">: <em></em> <strong>inputParser.Parameters</strong></dt>
<dd><p>Return list of parameter names already defined.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eResults">: <em></em> <strong>inputParser.Results</strong></dt>
<dd><p>Return structure with argument names as fieldnames and corresponding values.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eUnmatched">: <em></em> <strong>inputParser.Unmatched</strong></dt>
<dd><p>Return structure similar to <code>Results</code>, but for unmatched parameters.
See the <code>KeepUnmatched</code> property.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eUsingDefaults">: <em></em> <strong>inputParser.UsingDefaults</strong></dt>
<dd><p>Return cell array with the names of arguments that are using default values.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eCaseSensitive">: <em></em> <strong>inputParser.CaseSensitive</strong> <em>= <var>boolean</var></em></dt>
<dd><p>Set whether matching of argument names should be case sensitive.  Defaults
to false.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eFunctionName">: <em></em> <strong>inputParser.FunctionName</strong> <em>= <var>name</var></em></dt>
<dd><p>Set function name to be used in error messages; Defaults to empty string.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eKeepUnmatched">: <em></em> <strong>inputParser.KeepUnmatched</strong> <em>= <var>boolean</var></em></dt>
<dd><p>Set whether an error should be given for non-defined arguments.  Defaults to
false.  If set to true, the extra arguments can be accessed through
<code>Unmatched</code> after the <code>parse</code> method.  Note that since
<code>Switch</code> and <code>Parameter</code> arguments can be mixed, it is
not possible to know the unmatched type.  If argument is found unmatched
it is assumed to be of the <code>Parameter</code> type and it is expected to
be followed by a value.
</p>
</dd></dl>
<dl>
<dt id="index-inputParser_002eStructExpand">: <em></em> <strong>inputParser.StructExpand</strong> <em>= <var>boolean</var></em></dt>
<dd><p>Set whether a structure can be passed to the function instead of
parameter/value pairs.  Defaults to true.
</p>
<p>The following example shows how to use this class:
</p>
<div class="example">
<pre class="example">function check (varargin)
  p = inputParser ();                      # create object
  p.FunctionName = &quot;check&quot;;                # set function name
  p.addRequired (&quot;pack&quot;, @ischar);         # mandatory argument
  p.addOptional (&quot;path&quot;, pwd(), @ischar);  # optional argument

  ## create a function handle to anonymous functions for validators
  val_mat = @(x) isvector (x) &amp;&amp; all (x &lt;= 1) &amp;&amp; all (x &gt;= 0);
  p.addOptional (&quot;mat&quot;, [0 0], val_mat);

  ## create two arguments of type &quot;Parameter&quot;
  val_type = @(x) any (strcmp (x, {&quot;linear&quot;, &quot;quadratic&quot;}));
  p.addParameter (&quot;type&quot;, &quot;linear&quot;, val_type);
  val_verb = @(x) any (strcmp (x, {&quot;low&quot;, &quot;medium&quot;, &quot;high&quot;}));
  p.addParameter (&quot;tolerance&quot;, &quot;low&quot;, val_verb);

  ## create a switch type of argument
  p.addSwitch (&quot;verbose&quot;);

  p.parse (varargin{:});  # Run created parser on inputs

  ## the rest of the function can access inputs by using p.Results.
  ## for example, get the tolerance input with p.Results.tolerance
endfunction
</pre></div>

<div class="example">
<pre class="example">check (&quot;mech&quot;);           # valid, use defaults for other arguments
check ();                 # error, one argument is mandatory
check (1);                # error, since ! ischar
check (&quot;mech&quot;, &quot;~/dev&quot;);  # valid, use defaults for other arguments

check (&quot;mech&quot;, &quot;~/dev&quot;, [0 1 0 0], &quot;type&quot;, &quot;linear&quot;);  # valid

## following is also valid.  Note how the Switch argument type can
## be mixed into or before the Parameter argument type (but it
## must still appear after any Optional argument).
check (&quot;mech&quot;, &quot;~/dev&quot;, [0 1 0 0], &quot;verbose&quot;, &quot;tolerance&quot;, &quot;high&quot;);

## following returns an error since not all optional arguments,
## 'path' and 'mat', were given before the named argument 'type'.
check (&quot;mech&quot;, &quot;~/dev&quot;, &quot;type&quot;, &quot;linear&quot;);
</pre></div>

<p><em>Note 1</em>: A function can have any mixture of the four API types but
they must appear in a specific order.  <code>Required</code> arguments must be
first and can be followed by any <code>Optional</code> arguments.  Only
the <code>Parameter</code> and <code>Switch</code> arguments may be mixed
together and they must appear at the end.
</p>
<p><em>Note 2</em>: If both <code>Optional</code> and <code>Parameter</code> arguments
are mixed in a function API then once a string Optional argument fails to
validate it will be considered the end of the <code>Optional</code>
arguments.  The remaining arguments will be compared against any
<code>Parameter</code> or <code>Switch</code> arguments.
</p>

<p><strong>See also:</strong> <a href="Defining-Functions.html#XREFnargin">nargin</a>, <a href="Validating-the-type-of-Arguments.html#XREFvalidateattributes">validateattributes</a>, <a href="Validating-the-type-of-Arguments.html#XREFvalidatestring">validatestring</a>, <a href="Variable_002dlength-Argument-Lists.html#XREFvarargin">varargin</a>.
</p></dd></dl>


<hr>
<div class="header">
<p>
Previous: <a href="Validating-the-type-of-Arguments.html" accesskey="p" rel="prev">Validating the type of Arguments</a>, Up: <a href="Validating-Arguments.html" accesskey="u" rel="up">Validating Arguments</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>