@@ -1705,23 +1705,7 @@ def __str__(self):
17051705 return self .value
17061706
17071707
1708- def main ():
1709- basicConfig (
1710- level = logging .WARNING ,
1711- stream = sys .stderr ,
1712- )
1713-
1714- StepLogger .start ()
1715- processwrapper .enable_logging ()
1716-
1717- # Support both legacy variables and properly namespaced ones
1718- place = os .environ .get ("PLACE" , None )
1719- place = os .environ .get ("LG_PLACE" , place )
1720- state = os .environ .get ("STATE" , None )
1721- state = os .environ .get ("LG_STATE" , state )
1722- initial_state = os .environ .get ("LG_INITIAL_STATE" , None )
1723- token = os .environ .get ("LG_TOKEN" , None )
1724-
1708+ def get_parser () -> argparse .ArgumentParser :
17251709 parser = argparse .ArgumentParser ()
17261710 parser .add_argument (
17271711 "-x" ,
@@ -1734,24 +1718,19 @@ def main():
17341718 "-c" ,
17351719 "--config" ,
17361720 type = str ,
1737- default = os .environ .get ("LG_ENV" ),
17381721 help = "env config file (default: value from env variable LG_ENV)" ,
17391722 )
1740- parser .add_argument (
1741- "-p" , "--place" , type = str , default = place , help = "place name/alias (default: value from env variable LG_PLACE)"
1742- )
1723+ parser .add_argument ("-p" , "--place" , type = str , help = "place name/alias (default: value from env variable LG_PLACE)" )
17431724 parser .add_argument (
17441725 "-s" ,
17451726 "--state" ,
17461727 type = str ,
1747- default = state ,
17481728 help = "strategy state to switch into before command (default: value from env varibale LG_STATE)" ,
17491729 )
17501730 parser .add_argument (
17511731 "-i" ,
17521732 "--initial-state" ,
17531733 type = str ,
1754- default = initial_state ,
17551734 help = "strategy state to force into before switching to desired state" ,
17561735 )
17571736 parser .add_argument (
@@ -2065,11 +2044,11 @@ def main():
20652044 subparser .set_defaults (func = ClientSession .create_reservation )
20662045
20672046 subparser = subparsers .add_parser ("cancel-reservation" , help = "cancel a reservation" )
2068- subparser .add_argument ("token" , type = str , default = token , nargs = "?" if token else None )
2047+ subparser .add_argument ("token" , type = str , nargs = "?" )
20692048 subparser .set_defaults (func = ClientSession .cancel_reservation )
20702049
20712050 subparser = subparsers .add_parser ("wait" , help = "wait for a reservation to be allocated" )
2072- subparser .add_argument ("token" , type = str , default = token , nargs = "?" if token else None )
2051+ subparser .add_argument ("token" , type = str , nargs = "?" )
20732052 subparser .set_defaults (func = ClientSession .wait_reservation )
20742053
20752054 subparser = subparsers .add_parser ("reservations" , help = "list current reservations" )
@@ -2092,13 +2071,55 @@ def main():
20922071 subparser = subparsers .add_parser ("version" , help = "show version" )
20932072 subparser .set_defaults (func = ClientSession .print_version )
20942073
2074+ return parser
2075+
2076+
2077+ def main ():
2078+ basicConfig (
2079+ level = logging .WARNING ,
2080+ stream = sys .stderr ,
2081+ )
2082+
2083+ StepLogger .start ()
2084+ processwrapper .enable_logging ()
2085+
2086+ # Support both legacy variables and properly namespaced ones
2087+ place = os .environ .get ("PLACE" , None )
2088+ place = os .environ .get ("LG_PLACE" , place )
2089+ state = os .environ .get ("STATE" , None )
2090+ state = os .environ .get ("LG_STATE" , state )
2091+ initial_state = os .environ .get ("LG_INITIAL_STATE" , None )
2092+ token = os .environ .get ("LG_TOKEN" , None )
2093+
2094+ parser = get_parser ()
2095+
20952096 # make any leftover arguments available for some commands
20962097 args , leftover = parser .parse_known_args ()
20972098 if args .command not in ["ssh" , "rsync" , "forward" ]:
20982099 args = parser .parse_args ()
20992100 else :
21002101 args .leftover = leftover
21012102
2103+ # handle dynamic defaults
2104+ if args .config is None :
2105+ args .config = os .environ .get ("LG_ENV" )
2106+
2107+ if args .place is None :
2108+ args .place = place
2109+
2110+ if args .state is None :
2111+ args .state = state
2112+
2113+ if args .initial_state is None :
2114+ args .initial_state = initial_state
2115+
2116+ if args .command in ["cancel-reservation" , "wait" ] and args .token is None :
2117+ if token :
2118+ args .token = token
2119+ else :
2120+ print ("Please provide a token" , file = sys .stderr )
2121+ exit (1 )
2122+
21022123 if args .verbose :
21032124 logging .getLogger ().setLevel (logging .INFO )
21042125 if args .verbose > 1 :
0 commit comments