@@ -75,17 +75,26 @@ def search(self, i):
7575 #indices
7676
7777
78- def mlcr ():
79- first_arg_value = "run"
80- second_arg_value = "script"
78+ def mlc_expand_short (action , target = "script" ):
8179
8280 # Insert the positional argument into sys.argv for the main function
83- sys .argv .insert (1 , first_arg_value )
84- sys .argv .insert (2 , second_arg_value )
81+ sys .argv .insert (1 , action )
82+ sys .argv .insert (2 , target )
8583
8684 # Call the main function
8785 main ()
8886
87+ def mlcr ():
88+ mlc_expand_short ("run" )
89+ def mlcd ():
90+ mlc_expand_short ("docker" )
91+ def mlce ():
92+ mlc_expand_short ("experiment" )
93+ def mlct ():
94+ mlc_expand_short ("test" )
95+
96+ def mlcp ():
97+ mlc_expand_short ("pull" , "repo" )
8998
9099
91100def process_console_output (res , target , action , run_args ):
@@ -132,12 +141,63 @@ def main():
132141
133142 For help related to a particular target, run:
134143
135- mlc help <target>
144+ mlc <target> --help/-h
145+
146+ Examples:
147+ mlc script --help
148+ mlc repo -h
136149
137150 For help related to a specific action for a target, run:
138151
139- mlc help <action> <target>
152+ mlc <action> <target> --help/-h
153+ Examples:
154+ mlc run script --help
155+ mlc pull repo -h
140156 """
157+
158+ # First level parser for showing help
159+ pre_parser = argparse .ArgumentParser (add_help = False )
160+ pre_parser .add_argument ("action" , nargs = "?" , help = "Top-level action (run, build, help, etc.)" )
161+ pre_parser .add_argument ("target" , choices = ['run' , 'script' , 'cache' , 'repo' ], nargs = "?" , help = "Potential target (repo, script, cache, ...)" )
162+ pre_parser .add_argument ("-h" , "--help" , action = "store_true" )
163+ pre_args , remaining_args = pre_parser .parse_known_args ()
164+
165+ if pre_args .help and not any ("--tags" in arg for arg in remaining_args ):
166+ help_text = ""
167+ if pre_args .target == "run" :
168+ if pre_args .action == "docker" :
169+ pre_args .target = "script"
170+ else :
171+ logger .error (f"Invalid action-target { pre_args .action } - { pre_args .target } combination" )
172+ raise Exception (f"Invalid action-target { pre_args .action } - { pre_args .target } combination" )
173+ if not pre_args .action and not pre_args .target :
174+ help_text += main .__doc__
175+ elif pre_args .action and not pre_args .target :
176+ if pre_args .action not in ['script' , 'cache' , 'repo' ]:
177+ logger .error (f"Invalid target { pre_args .action } " )
178+ raise Exception (f"""Invalid target { pre_args .action } """ )
179+ else :
180+ pre_args .target , pre_args .action = pre_args .action , None
181+ actions = get_action (pre_args .target , default_parent )
182+ help_text += actions .__doc__
183+ # iterate through every method
184+ for method_name , method in inspect .getmembers (actions .__class__ , inspect .isfunction ):
185+ method = getattr (actions , method_name )
186+ if method .__doc__ and not method .__doc__ .startswith ("_" ):
187+ help_text += method .__doc__
188+ elif pre_args .action and pre_args .target :
189+ actions = get_action (pre_args .target , default_parent )
190+ try :
191+ method = getattr (actions , pre_args .action )
192+ help_text += actions .__doc__
193+ help_text += method .__doc__
194+ except :
195+ logger .error (f"Error: '{ pre_args .action } ' is not supported for { pre_args .target } ." )
196+ if help_text != "" :
197+ print (help_text )
198+ sys .exit (0 )
199+
200+ # parser for execution of the automation scripts
141201 parser = argparse .ArgumentParser (prog = 'mlc' , description = 'A CLI tool for managing repos, scripts, and caches.' , add_help = False )
142202
143203 # Subparsers are added to main parser, allowing for different commands (subcommands) to be defined.
@@ -146,30 +206,24 @@ def main():
146206
147207 # Script and Cache-specific subcommands
148208 for action in ['run' , 'pull' , 'test' , 'add' , 'show' , 'list' , 'find' , 'search' , 'rm' , 'cp' , 'mv' ]:
149- action_parser = subparsers .add_parser (action , help = f' { action } a target.' )
209+ action_parser = subparsers .add_parser (action , add_help = False )
150210 action_parser .add_argument ('target' , choices = ['repo' , 'script' , 'cache' ], help = 'Target type (repo, script, cache).' )
151211 # the argument given after target and before any extra options like --tags will be stored in "details"
152212 action_parser .add_argument ('details' , nargs = '?' , help = 'Details or identifier (optional for list).' )
153213 action_parser .add_argument ('extra' , nargs = argparse .REMAINDER , help = 'Extra options (e.g., -v)' )
154214
155215 # Script specific subcommands
156216 for action in ['docker' , 'experiment' , 'doc' , 'lint' ]:
157- action_parser = subparsers .add_parser (action , help = f' { action . capitalize () } a target.' )
217+ action_parser = subparsers .add_parser (action , add_help = False )
158218 action_parser .add_argument ('target' , choices = ['script' , 'run' ], help = 'Target type (script).' )
159219 # the argument given after target and before any extra options like --tags will be stored in "details"
160220 action_parser .add_argument ('details' , nargs = '?' , help = 'Details or identifier (optional for list).' )
161221 action_parser .add_argument ('extra' , nargs = argparse .REMAINDER , help = 'Extra options (e.g., -v)' )
162222
163223 for action in ['load' ]:
164- load_parser = subparsers .add_parser (action , help = f' { action . capitalize () } a target.' )
224+ load_parser = subparsers .add_parser (action , add_help = False )
165225 load_parser .add_argument ('target' , choices = ['cfg' ], help = 'Target type (cfg).' )
166226
167- for action in ['help' ]:
168- action_parser = subparsers .add_parser (action , help = f'{ action .capitalize ()} a target.' )
169- action_parser .add_argument ('action' , help = 'action type (run).' , nargs = '?' , default = None )
170- action_parser .add_argument ('target' , choices = ['script' , 'cache' , 'repo' ], help = 'Target type (script).' , nargs = '?' , default = None )
171- action_parser .add_argument ('extra' , nargs = argparse .REMAINDER , help = 'Extra options (e.g., -v)' )
172-
173227 # Parse arguments
174228 args = parser .parse_args ()
175229
@@ -201,45 +255,7 @@ def main():
201255
202256 run_args = res ['args_dict' ]
203257
204- # handle help in mlcflow
205- if args .command in ['help' ]:
206- help_text = ""
207- if not args .action and not args .target :
208- help_text += main .__doc__
209- elif args .action and not args .target :
210- if args .action not in ['script' , 'cache' , 'repo' ]:
211- logger .error (f"Invalid target { args .action } " )
212- raise Exception (f"""Invalid target { args .action } """ )
213- else :
214- args .target , args .action = args .action , None
215- actions = get_action (args .target , default_parent )
216- help_text += actions .__doc__
217- # iterate through every method
218- for method_name , method in inspect .getmembers (actions .__class__ , inspect .isfunction ):
219- method = getattr (actions , method_name )
220- if method .__doc__ and not method .__doc__ .startswith ("_" ):
221- help_text += method .__doc__
222- elif args .action and args .target and run_args .get ('tags' ):
223- actions = get_action (args .target , default_parent )
224- if actions and hasattr (actions , args .command ):
225- method = getattr (actions , args .command )
226- res = method (run_args )
227- if res ['return' ] > 0 :
228- logger .error (res .get ('error' , f"Error in { action } " ))
229- raise Exception (f"""An error occurred { res } """ )
230- else :
231- logger .error (f"Error: '{ args .command } ' is not supported for { args .target } ." )
232- elif args .action and args .target :
233- actions = get_action (args .target , default_parent )
234- try :
235- method = getattr (actions , args .action )
236- help_text += actions .__doc__
237- help_text += method .__doc__
238- except :
239- logger .error (f"Error: '{ args .action } ' is not supported for { args .target } ." )
240- if help_text != "" :
241- print (help_text )
242- sys .exit (0 )
258+ run_args ['mlc_run_cmd' ] = " " .join (sys .argv )
243259
244260 if hasattr (args , 'repo' ) and args .repo :
245261 run_args ['repo' ] = args .repo
0 commit comments