Skip to content
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

Fix invalid parsing of the bool arguments #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/pyff/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def short_spec(self):

def long_spec(self):
long_name = self.long_name
if hasattr(self, 'typeconv') and self.typeconv == as_bool:
if (hasattr(self, 'typeconv') and self.typeconv == as_bool) or isinstance(self, InvertedSetting):
return '{}'.format(long_name)
else:
return '{}='.format(long_name)
Expand Down Expand Up @@ -201,10 +201,10 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def __get__(self, instance, owner):
return not self.setting.__get__()
return not self.setting.__get__(instance, owner)

def __set__(self, instance, value):
self.setting.__set__(not value)
self.setting.__set__(instance, not value)


class DummySetting(BaseSetting):
Expand Down Expand Up @@ -292,7 +292,7 @@ class Config(object):

no_caching = N('no_caching', invert=caching_enabled, short='C', info="disable all caches")

daemonize = S("daemonize", default=True, cmdline=['pyffd'], info="run in background")
daemonize = S("daemonize", default=True, typeconv=as_bool, cmdline=['pyffd'], info="run in background")

foreground = N('foreground', invert=daemonize, short='f', cmdline=['pyffd'], info="run in foreground")

Expand Down Expand Up @@ -546,14 +546,22 @@ def parse_options(program, docs):
config.aliases[a] = uri
elif o in ('-m', '--module'):
config.modules.append(a)
elif o in ('-f', '--foreground'):
config.foreground = True
elif o in ('-C', '--no_caching'):
config.no_caching = True
else:
o = o.lstrip('-')
s = config.find_setting(o)
if s is not None:
if s.deprecated:
print("WARNING: {} is deprecated. Setting this option has no effect!".format(o))
else:
setattr(s, 'value', a)
value = a
if s.typeconv == as_bool:
value = True

setattr(s, 'value', value)
else:
raise ValueError("Unknown option {}".format(o))

Expand Down
28 changes: 28 additions & 0 deletions src/pyff/test/test_parse_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys

from unittest import TestCase
from unittest.mock import patch

from pyff.constants import parse_options, config


class TestParseOptions(TestCase):

def test_bool_long_spec_setting(self):
test_args = ["pyffd", "--devel_memory_profile", "--foreground"]

with patch.object(sys, 'argv', test_args):
parse_options("pyffd", "Additional help.")

self.assertTrue(config.devel_memory_profile)
self.assertTrue(config.foreground)
self.assertFalse(config.daemonize)

def test_inverted_setting_short_spec(self):
test_args = ["pyffd", "-C"]

with patch.object(sys, 'argv', test_args):
parse_options("pyffd", "Additional help.")

self.assertTrue(config.no_caching)
self.assertFalse(config.caching_enabled)