-
Notifications
You must be signed in to change notification settings - Fork 240
/
command_template
67 lines (51 loc) · 2.21 KB
/
command_template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Setup functions for the 'openmdao {command_name}' command plugin.
This file has been automatically generated and the command found here
just prints the command name the number of times specified at the command line immediately
after Problem.final_setup() runs.
"""
from openmdao.utils.hooks import _register_hook
from openmdao.utils.file_utils import _load_and_exec
from openmdao.utils.mpi import MPI
def _{command_name}_setup_parser(parser):
"""
Set up the openmdao subparser (using argparse) for the 'openmdao {command_name}' command.
Parameters
----------
parser : argparse subparser
The parser we're adding options to.
"""
# replace the following argument definitions with those appropriate for your command line tool
parser.add_argument('-r', '--repeat', action='store', dest='repeats',
default=1, type=int, help='Number of times to repeat command.')
parser.add_argument('file', metavar='file', nargs=1, help='Script to execute.')
def _{command_name}_exec(options, user_args):
"""
This registers the hook function and executes the user script.
Parameters
----------
options : argparse Namespace
Command line options.
user_args : list of str
Args to be passed to the user script.
"""
script = options.file[0] # this assumes that the parser arg 'file' was added above
def _{command_name}_run(prob):
if not MPI or MPI.COMM_WORLD.rank == 0:
for i in range(options.repeats):
print('*** {command_name} ***')
exit() # If you want the program to exit after your command, you must explicitly do that here
# register the hook to execute after Problem.final_setup
_register_hook('final_setup', class_name='Problem', post=_{command_name}_run)
# load and execute the given script as __main__
_load_and_exec(script, user_args)
def _{command_name}_setup():
"""
This is registered as an 'openmdao_commands' entry point.
It should return a tuple of the form (setup_parser_func, exec_func, help_string).
"""
return (
_{command_name}_setup_parser,
_{command_name}_exec,
"Print {command_name} 'r' times after final setup."
)