argparse Functions

generic argparse/add-option (ap ArgumentParser) names arg-data docstr

add an option to ap

Param ap:

ArgumentParser instance

Type ap:

ArgumentParser

Param names:

option name(s)

Type names:

list of symbols

Param arg-data:

argument data

Type arg-data:

see below

Param docstr:

documentation string

Type docstr:

string

Return:

#<unspec>

arg-data can be:

  • either one of:

    • :none meaning the option takes no argument

    • :optional meaning the option may take an argument, defaulting to #t

    • :required meaning the option must take an argument

    where the nominal argument name is 'ARG" for use in the usage string

  • or a list of :optional or :required and the nominal argument name as a string

Example:

ap := make-instance ArgumentParser :synopsis (sprintf "%s [options] [FRUIT ...]" ARGV0)

add-option ap '(a apple) :none "add an apple"
add-option ap '(o output) (:required "FILE") "direct output to FILE"
generic argparse/parse-args (ap ArgumentParser) argv

parse the argument in argv using the options associated with ap

Param ap:

ArgumentParser instance

Type ap:

ArgumentParser

Param argv:

argument strings

Type argv:

array of strings (

Return:

#<unspec>

Subsequently, ap.options will be a hash table with a key for each name defined in the options associated with ap and ap.operands will be a list of the remaining arguments.

generic argparse/usage (ap ArgumentParser)

print the usage for the options associated with ap

Param ap:

ArgumentParser instance

Type ap:

ArgumentParser

Return:

#<unspec>

Example:

For the options added above in a script called x.idio:

usage ap
  1. when passed the argument -h

    usage: x [options] [FRUIT ...]
    
    Options:
         --help, -h, -?       display this message and exit
     -a, --apple              option a
     -o, --output=FILE        direct output to FILE
    
  2. when passed the arguments -a -o foo

    usage: x [options] [FRUIT ...]
    
    Options:
         --help, -h, -?       display this message and exit
     -a, --apple              option a [#t]
     -o, --output=FILE        direct output to FILE [foo]
    

Last built at 2024-05-17T06:10:43Z+0000 from 62cca4c (dev) for Idio 0.3.b.6