Option parser

版本 1.0.0.0 (3.1 KB) 作者: Peter Corke
A generalized option parser for Toolbox functions
443.0 次下载
更新时间 2013/2/19

查看许可证

Supports keyword options that have a value, are a boolean flag, or enumeration types (string or int).

The software pattern is:

function(a, b, c, varargin)
opt.foo = false;
opt.bar = true;
opt.blah = [];
opt.choose = {'this', 'that', 'other'};
opt.select = {'#no', '#yes'};
opt = optparse(opt, varargin);

Optional arguments to the function behave as follows:
'foo' sets opt.foo <- true
'nobar' sets opt.foo <- false
'blah', 3 sets opt.blah <- 3
'blah', {x,y} sets opt.blah <- {x,y}
'that' sets opt.choose <- 'that'
'yes' sets opt.select <- 2 (the second element)

and can be given in any combination.

If neither of 'this', 'that' or 'other' are specified then opt.choose <- 'this'.

Alternatively if:
opt.choose = {[], 'this', 'that', 'other'};
then if neither of 'this', 'that' or 'other' are specified then opt.choose <- []

If neither of 'no' or 'yes' are specified then opt.select <- 1.

Note:
- That the enumerator names must be distinct from the field names.
- That only one value can be assigned to a field, if multiple values are required they must be converted to a cell array.
- To match an option that starts with a digit, prefix it with 'd_', so the field 'd_3d' matches the option '3d'.
The allowable options are specified by the names of the fields in the structure opt. By default if an option is given that is not a field of opt an error is declared.

Sometimes it is useful to collect the unassigned options and this can be achieved using a second output argument
[opt,arglist] = tb_optparse(opt, varargin);
which is a cell array of all unassigned arguments in the order given in varargin.

引用格式

Peter Corke (2024). Option parser (https://www.mathworks.com/matlabcentral/fileexchange/40383-option-parser), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2012b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0