Python: argparse optional arguments without dashes

There is no way to get argparse to do this for you. However, you can make argparse accept any number of positional arguments: parser.add_argument(‘FILES’,nargs=”*”) options=parser.parse_args() file1,optional_files=options.FILES[0],options.FILES[1:] Of course, you may want to add some checks to make sure that at least 1 file was given, etc. EDIT I’m still not 100% sure what you want … Read more

getopt does not parse optional arguments to parameters

Although not mentioned in glibc documentation or getopt man page, optional arguments to long style command line parameters require ‘equals sign’ (=). Space separating the optional argument from the parameter does not work. An example run with the test code: $ ./respond –praise John Kudos to John $ ./respond –praise=John Kudos to John $ ./respond … Read more

LaTeX Optional Arguments

Example from the guide: \newcommand{\example}[2][YYY]{Mandatory arg: #2; Optional arg: #1.} This defines \example to be a command with two arguments, referred to as #1 and #2 in the {<definition>}–nothing new so far. But by adding a second optional argument to this \newcommand (the [YYY]) the first argument (#1) of the newly defined command \example is … Read more

Named tuple and default values for optional keyword arguments

Python 3.7 Use the defaults parameter. >>> from collections import namedtuple >>> fields = (‘val’, ‘left’, ‘right’) >>> Node = namedtuple(‘Node’, fields, defaults=(None,) * len(fields)) >>> Node() Node(val=None, left=None, right=None) Or better yet, use the new dataclasses library, which is much nicer than namedtuple. >>> from dataclasses import dataclass >>> from typing import Any >>> … Read more

tech