punkshell_module_punk::args - args parsing
Utilities for parsing proc args
overview of punk::args
There are 2 main conventions for parsing a proc args list
leading option-value pairs followed by a list of values (Tk style)
leading list of values followed by option-value pairs (Tcl style)
punk::args is focused on the 1st convention (Tk style): parsing of args in leading option-value pair style - even for non-Tk usage.
The proc can still contain some leading required values e.g
proc dostuff {arg1 arg2 args} {...}}
but having the core values elements at the end of args is more generally useful - especially in cases where the number of trailing values is unknown and/or the proc is to be called in a functional 'pipeline' style.
The basic principle is that a call to punk::args::opts_vals is made near the beginning of the proc e.g
proc dofilestuff {args} { lassign [dict values [punk::args { -directory -default "" -translation -default binary } $args]] opts values puts "translation is [dict get $opts -translation]" foreach f [dict values $values] { puts "doing stuff with file: $f" } }
There are alternative args parsing packages such as:
argp
The tcllib set of TEPAM modules
TEPAM requires an alternative procedure declaration syntax instead of proc - but has support for Tk and documentation generation.
punk::args was designed initially without specific reference to TEPAM - and to handle some edge cases in specific projects where TEPAM wasn't suitable.
In subsequent revisions of punk::args - some features were made to operate in a way that is similar to TEPAM - to avoid gratuitous differences where possible, but of course there are differences
and those used TEPAM or mixing TEPAM and punk::args should take care to assess the differences.
TEPAM is a mature solution and is widely available as it is included in tcllib.
Serious consideration should be given to using TEPAM if suitable for your project.
Core API functions for punk::args
Parse rawargs as a sequence of zero or more option-value pairs followed by zero or more values
Returns a dict of the form: opts <options_dict> values <values_dict>
ARGUMENTS:
This a block of text with records delimited by newlines (lf or crlf) - but with multiline values allowed if properly quoted/braced
'info complete' is used to determine if a record spans multiple lines due to multiline values
Each optionspec line must be of the form:
-optionname -key val -key2 val2...
where the valid keys for each option specification are: -default -type -range -choices -optional
This is a list of the arguments to parse. Usually it will be the \$args value from the containing proc
Internal functions that are not part of the API
Copyright © 2024