2 .. include:: ../rst_prolog.rst
5 Add a user custom command
6 ***************************
11 .. note:: This documentation is for Python_ developers.
14 The salomeTools product provides a simple way to develop commands.
15 The first thing to do is to add a file with *.py* extension in the ``commands`` directory of salomeTools.
17 Here are the basic requirements that must be followed in this file in order to add a command.
22 By adding a file *mycommand.py* in the ``commands`` directory, salomeTools will define a new command named ``mycommand``.
24 In *mycommand.py*, there must be the following method: ::
26 def run(args, runner, logger):
30 In fact, at this point, the command will already be functional.
31 But there are some useful services provided by salomeTools :
33 * You can give some options to your command:
35 .. code-block:: python
39 # Define all possible option for mycommand command : 'sat mycommand <options>'
40 parser = src.options.Options()
41 parser.add_option('m', 'myoption', \
42 'boolean', 'myoption', \
43 'My option changes the behavior of my command.')
45 def run(args, runner, logger):
47 (options, args) = parser.parse_args(args)
51 * You can add a *description* method that will display a message when the user will call the help:
54 .. code-block:: python
55 :emphasize-lines: 9,10
59 # Define all possible option for mycommand command : 'sat mycommand <options>'
60 parser = src.options.Options()
61 parser.add_option('m', 'myoption', \
62 'boolean', 'myoption', \
63 'My option changes the behavior of my command.')
66 return _("The help of mycommand.")
68 def run(args, runner, logger):
70 (options, args) = parser.parse_args(args)
73 HowTo access salomeTools config and other commands
74 ========================================================
76 The *runner* variable is an python instance of *Sat* class.
77 It gives access to *runner.cfg* which is the data model defined from all
78 *configuration pyconf files* of salomeTools
79 For example, *runner.cfg.APPLICATION.workdir*
80 contains the root directory of the current application.
82 The *runner* variable gives also access to other commands of salomeTools:
84 .. code-block:: python
86 # as CLI_ 'sat prepare ...'
87 runner.prepare(runner.cfg.VARS.application)
92 The logger variable is an instance of the Logger class.
93 It gives access to the write method.
95 When this method is called, the message passed as parameter
96 will be displayed in the terminal and written in an xml log file.
98 .. code-block:: python
100 logger.write("My message", 3) # 3 as default
102 The second argument defines the level of verbosity
103 that is wanted for this message.
104 It has to be between 1 and 5 (the most verbose level).
109 Here is a *hello* command, file *commands/hello.py*:
111 .. code-block:: python
117 Define all possible options for hello command:
121 parser = src.options.Options()
122 parser.add_option('f', 'french', 'boolean', 'french', "french set hello message in french.")
125 return _("The help of hello.")
127 def run(args, runner, logger):
129 (options, args) = parser.parse_args(args)
131 if not options.french:
132 logger.write('HELLO! WORLD!\n')
134 logger.write('Bonjour tout le monde!\n')
136 A first call of hello:
140 # Get the help of hello:
145 Bonjour tout le monde!