File tree Expand file tree Collapse file tree 3 files changed +23
-1
lines changed
Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 77
88@click .command ()
99@click .option ("-f" , "--flag" )
10- def example (flag ):
10+ @click .option ("-t" , "--test" , default = "not set" )
11+ def example (flag , test , default_kwd = None ):
1112 """🧪 Example custom command.
1213
1314 Accepts arbitrary flags, and shows how to access `pyproject.toml`
@@ -20,6 +21,11 @@ def example(flag):
2021 click .secho ("Flag provided with --flag is: " , fg = "yellow" , nl = False )
2122 print (flag or None )
2223
24+ click .secho ("Flag provided with --test is: " , fg = "yellow" , nl = False )
25+ print (test or None )
26+
27+ click .secho (f"Default kwd is: { default_kwd } " )
28+
2329 click .secho ("\n Defined commands:" , fg = "yellow" )
2430 for section in commands :
2531 print (f" { section } : " , end = "" )
Original file line number Diff line number Diff line change @@ -48,3 +48,6 @@ package = 'example_pkg'
4848"Install" = [
4949 " spin.cmds.pip.install"
5050]
51+
52+ [tool .spin .kwargs ]
53+ ".spin/cmds.py:example" = {"test" = " default override" , "default_kwd" = 3 }
Original file line number Diff line number Diff line change @@ -94,6 +94,7 @@ def group(ctx):
9494 "spin.python" : _cmds .meson .python ,
9595 "spin.shell" : _cmds .meson .shell ,
9696 }
97+ cmd_default_kwargs = toml_config .get ("tool.spin.kwargs" , {})
9798
9899 for section , cmds in config_cmds .items ():
99100 for cmd in cmds :
@@ -133,6 +134,18 @@ def group(ctx):
133134 print (f"!! Could not load command `{ func } ` from file `{ path } `.\n " )
134135 continue
135136
137+ default_kwargs = cmd_default_kwargs .get (cmd )
138+ import functools
139+
140+ if default_kwargs :
141+ callback = cmd_func .callback
142+ cmd_func .callback = functools .partial (callback , ** default_kwargs )
143+
144+ # Also override option defaults
145+ for option in cmd_func .params :
146+ if option .name in default_kwargs :
147+ option .default = default_kwargs [option .name ]
148+
136149 commands [cmd ] = cmd_func
137150
138151 group .add_command (commands [cmd ], section = section )
You can’t perform that action at this time.
0 commit comments