8000 Support boolean config values on the command line by rknop · Pull Request #8 · Roman-Supernova-PIT/snpit_utils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support boolean config values on the command line #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations 8000
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/8.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support boolean config values on the command line
7 changes: 5 additions & 2 deletions snpit_utils/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import argparse
import copy
import numbers
import os
import pathlib
import numbers
import types
import copy
import yaml

from snpit_utils.logger import SNLogger
Expand Down Expand Up @@ -822,6 +823,8 @@ def augment_argparse( self, parser, path='', _dict=None ):
parser.add_argument( f'--{path}{key}', nargs="*", help=f"Default: {val}" )
elif isinstance( val, str ):
parser.add_argument( f'--{path}{key}', help=f"Default: {val}" )
elif isinstance( val, bool ):
parser.add_argument( f'--{path}{key}', action=argparse.BooleanOptionalAction, help=f"Default: {val}" )
elif isinstance( val, numbers.Integral ):
parser.add_argument( f'--{path}{key}', type=int, help=f"Default: {val}" )
elif isinstance( val, numbers.Real ):
Expand Down
2 changes: 2 additions & 0 deletions snpit_utils/tests/config_test_data/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ maindict:
mainval1: val1
mainval2: val2
mainval3: val3
bool_value: True
false_bool_value: False

mainlist1:
- main1
Expand Down
32 changes: 31 additions & 1 deletion snpit_utils/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def test_loading_and_getting( cfg ):
assert cfg.value( 'replpreload2scalar2' ) == '2scalar2'

# maindict is in test.yaml and not modified
assert cfg.value( 'maindict' ) == { 'mainval1': 'val1', 'mainval2': 'val2', 'mainval3': 'val3' }
assert cfg.value( 'maindict' ) == { 'mainval1': 'val1', 'mainval2': 'val2', 'mainval3': 'val3',
'bool_value': True, 'false_bool_value': False }
assert cfg.value( 'maindict.mainval2' ) == 'val2'

# mainlist1 is in test.yaml and not modified
Expand Down Expand Up @@ -275,6 +276,35 @@ def test_command_line( cfg ):
assert cfg.value( 'nest.nest1' ) == [ 'mouse', 'wombat' ]


def test_command_line_boolean_not_given( cfg ):
parser = argparse.ArgumentParser()
cfg.augment_argparse( parser )
arglist = []
args = parser.parse_args( arglist )
cfg.parse_args( args )
assert cfg.value( 'maindict.bool_value' )
assert not cfg.value( 'maindict.false_bool_value' )


def test_command_line_boolean_set_true( cfg ):
parser = argparse.ArgumentParser()
cfg.augment_argparse( parser )
arglist = [ '--maindict-bool_value', '--maindict-false_bool_value' ]
args = parser.parse_args( arglist )
cfg.parse_args( args )
assert cfg.value( 'maindict.bool_value' )
assert cfg.value( 'maindict.false_bool_value' )


def test_command_line_boolean_set_false( cfg ):
parser = argparse.ArgumentParser()
cfg.augment_argparse( parser )
arglist = [ '--no-maindict-bool_value', '--no-maindict-false_bool_value' ]
args = parser.parse_args( arglist )
cfg.parse_args( args )
assert not cfg.value( 'maindict.bool_value' )
assert not cfg.value( 'maindict.false_bool_value' )


def test_no_direct_instantiation():
with pytest.raises( RuntimeError, match="Don't instantiate a Config directly; use configobj=Config.get(...)." ):
Expand Down
0