Just for general interest, here's my little argument parser:

import sys

def getopt (param, default=None):
    """Return a named parameter value from the command line.

    The parameter and its value must be of the form

        -param value

    The parameter string and its following value are removed 
    from sys.argv. If the parameter is not present on the command line,
    then the value of the variable is not changed, and nothing
    is removed from sys.argv. 

    Param can be a flag (that is, a boolean will be returned and the
    value on the command line following the flag will not be returned
    or consumed) if the "default" parameter is omitted or None. 
    Typical code for this usage would be:

    WANT_HELP = getopt('-h') 
    if WANT_HELP:
        print(__doc__)
        sys.exit(1)

    NOTE: this function converts the result type to the type
    of "default" - e.g., int(), float(), or bool().

    ARGUMENTS
    param -- the name of the command line option, including the 
             '-' character if used.
    default -- the default value for the parameter.  If omitted,
               the function returns True if the param is present.

    RETURNS
    the new value of the parameter, or the default value if param
    is missing from the command line, or True if param is a flag 
    (i.e., "default" is omitted or None).
    """

    try:
        opt = sys.argv.index(param)
        del sys.argv[opt]
        if default is not None:
            option = type(default)(sys.argv.pop(opt))
        else:
            option = True # don't consume the next parameter
    except ValueError:
        option = default # param is not in sys.argv
    except IndexError:
        msg = '"%s" parameter is missing its required value' % param
        raise ValueError(msg)
    return option
On Thursday, June 15, 2023 at 8:50:14 AM UTC-4 Thomas Passin wrote:

> I wrote my own a few years ago.  It was simple to use and understand.  It 
> was too simple, perhaps, but it did what I needed for a range of small 
> programs.  The only thing it doesn't do is to flag unknown options: it 
> ignores them instead.
>
> I say, if you don't need all the complexity of argparse don't use it!
>
> On Thursday, June 15, 2023 at 5:36:55 AM UTC-4 Edward K. Ream wrote:
>
>> On Thursday, June 15, 2023 at 3:36:13 AM UTC-5 Edward K. Ream wrote:
>>
>> I plan to rewrite LM.scanOptions without argparse.
>>
>>
>> Initial work is promising.  *Everything* seems easier w/o argparse!  See 
>> PR #3382 <https://github.com/leo-editor/leo-editor/pull/3382>.
>>
>> Edward
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/806cc6bd-1c1e-4cb9-9cf2-8ffe2eb3cd1bn%40googlegroups.com.

Reply via email to