7 The salomeTools product provides a simple way to develop commands.
8 The first thing to do is to add a file with ".py" extension in the **commands** directory of salomeTools.
10 Here are the basic requirements that must be followed in this file in order to add a command.
15 By adding a file **mycommand.py** in the **commands** directory, salomeTools will define a new command named **mycommand**.
17 In **mycommand.py**, there must be the following method: ::
19 def run(args, runner, logger):
23 In fact, at this point, the command will already be functional.
24 But there are some useful services provided by salomeTools :
26 * You can give some options to your command:
28 .. code-block:: python
29 :emphasize-lines: 1,3,4,5,8,9
33 # Define all possible option for mycommand command : sat mycommand <options>
34 parser = src.options.Options()
35 parser.add_option('m', 'myoption', 'boolean', 'myoption', "My option changes the behavior of my command.")
37 def run(args, runner, logger):
39 (options, args) = parser.parse_args(args)
43 * You can add a **description** method that will display a message when the user will call the help:
46 .. code-block:: python
51 # Define all possible option for mycommand command : sat mycommand <options>
52 parser = src.options.Options()
53 parser.add_option('m', 'myoption', 'boolean', 'myoption', "My option changes the behavior of my command.")
56 return _("The help of mycommand.")
58 def run(args, runner, logger):
60 (options, args) = parser.parse_args(args)
63 How to access to salomeTools config and other commands ?
64 ========================================================
66 The runner variable is an instance of the Sat class. It gives access to cfg : the data model that is read from all configuration files of salomeTools (*.pyconf) For example, runner.cfg.APPLICATION.out_dir will contain the root directory of the application on which you are working.
68 It gives also access to all other commands of salomeTools :
70 .. code-block:: python
72 runner.prepare(runner.cfg.VARS.application)
77 The logger variable is an instance of the Logger class. It gives access to the write method.
79 When this method is called, the message passed as parameter will be displayed in the termnial and written in an xml log file.
81 .. code-block:: python
83 logger.write("My message", 3)
85 The second argument defines the level of verbosity that is wanted for this message. It has to be between 1 and 5 (the most verbose level).
90 Here is a hello world command :
92 .. code-block:: python
96 # Define all possible option for mycommand command : sat mycommand <options>
97 parser = src.options.Options()
98 parser.add_option('m', 'myoption', 'boolean', 'myoption', "My option changes the behavior of my command.")
101 return _("The help of mycommand.")
103 def run(args, runner, logger):
105 (options, args) = parser.parse_args(args)
108 logger.write('HELLO, WORLD !\n')
110 logger.write('WORLD, HELLO !\n')
112 A first call of mycommand:
116 >./sat mycommand --myoption
119 Tap the following command to get the log :
120 /path/to/salomeTools/sat log
122 Another call of mycommand:
129 Tap the following command to get the log :
130 /path/to/salomeTools/sat log
132 Get the help of mycommand:
136 >./sat --help mycommand
140 The help of mycommand.
142 Available options are:
143 -m, --myoption (boolean)
144 My option changes the behavior of my command.