def test_basic(self): fset = FlagSet( { "test_str": Flag(str, cmdline_name="--test-str"), "test_bool": Flag(bool, cmdline_name=["--test-bool", "-b"]), "test_int": Flag(int, cmdline_name="--test-int"), "test_float": Flag( float, cmdline_name="--test-float", env_name="TEST_FLOAT" ), "test_nothing": Flag(default="hey", cmdline_name="-c"), } ) flags = fset.parse(["--test-str", "hi", "--test-bool"], use_exc=True) self.assertEqual(flags["test_str"], "hi") self.assertTrue(flags["test_bool"]) self.assertEqual(flags["test_nothing"], "hey") flags = fset.parse(["--test-bool", "false"], use_exc=True) self.assertFalse(flags["test_bool"]) flags = fset.parse(["-b"], use_exc=True) self.assertTrue(flags["test_bool"]) flags = fset.parse(["--test-int", "1000", "--test-float", "1.0"], use_exc=True) self.assertEqual(flags["test_int"], 1000) self.assertEqual(flags["test_float"], 1.0) flags = fset.parse([], env={"TEST_FLOAT": "1.1"}) self.assertEqual(flags["test_float"], 1.1)
def test_positional(self): fset = FlagSet({"flag": Flag(cmdline_name="flag-name")}) flags = fset.parse(args=["from_cmd"], use_exc=True) self.assertEqual(flags["flag"], "from_cmd") self.assertNotIn("flag-name", flags) # calling with -h flag with missing positional argument should not yield error output = io.StringIO() flags = fset.parse(args=["-h"], help_output_file=output, use_exc=True) print(output.getvalue())
def test_precedence(self): fset = FlagSet( {"flag": Flag(cmdline_name="--flag", env_name="FLAG", config_name="flag")} ) # cmdline flag > env flags = fset.parse( args=["--flag", "from cmd"], env={"FLAG": "from env"}, use_exc=True ) self.assertEqual(flags["flag"], "from cmd") # env > config file flags = fset.parse( args=["tests/_fixture/config2.json"], env={"FLAG": "from env"}, use_exc=True ) self.assertEqual(flags["flag"], "from env") # cmdline flag > config file flags = fset.parse( args=["--flag", "from cmd", "tests/_fixture/config2.json"], env={"FLAG": "from env"}, use_exc=True, ) self.assertEqual(flags["flag"], "from cmd")
from flagset import Flag, FlagSet fset = FlagSet({ "token": Flag( str, cmdline_name="token", env_name="BROADWAY_TOKEN", config_name="token", help="cluster token", ), "grader_id": Flag( str, cmdline_name="grader-id", env_name="BROADWAY_GRADER_ID", config_name="id", help="unique identifier of the grader instance", ), "api_host": Flag( str, default="http://127.0.0.1:1470", cmdline_name="--api-host", env_name="BROADWAY_API_HOST", config_name="api_host", help="api host. no slash in the end. " + "supported protocols: ws(s) and http(s)", ), "verbose": Flag(
from flagset import Flag, FlagSet app_flags = FlagSet({ "debug": Flag( bool, default=False, cmdline_name=["-d", "--debug"], env_name="BROADWAY_DEBUG", config_name="debug", help="enable debug mode", ), "token": Flag( str, cmdline_name="--token", env_name="BROADWAY_TOKEN", config_name="token", help="access token for communication between workers and api", ), "heartbeat_interval": Flag( int, default=10, cmdline_name="--heartbeat-interval", config_name="heartbeat_interval", help="heartbeat interval in seconds", ), "course_config": Flag( str,
def test_constraint(self): with self.assertRaises(AssertionError): FlagSet({ "required": Flag(cmdline_name="-r", required=True, default="no") })
def test_require(self): fset = FlagSet({"required": Flag(cmdline_name="-r", required=True)}) with self.assertRaises(FlagParseError): fset.parse([], use_exc=True)