[Tutor] How to update only set CLI args?

Cameron Simpson cs at cskk.id.au
Mon Jan 17 20:19:09 EST 2022


On 17Jan2022 17:11, Leam Hall <leamhall at gmail.com> wrote:
>This is what I came up with, after my work day was done.  :)
>
>###
>
>def sort_args(defaults, config, args):
>  """ Returns dict of configuration, with defaults lowest priority,
>      then config file options, then CLI args as highest.
>  """
>  defaults = copy.deepcopy(defaults)
>  defaults.update(config)
>  for key, value in vars(args).items():
>    if value:

This won't do the right thing if value is eg 0. If you know an omitted 
CLI option will be None (ideally it would not even be present) then do a 
precise check for None, not "falsiness".

    if value is not None:

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list