Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Declaring and Using Command-Line Arguments in Op Scripts

 

Junos OS op scripts can accept command-line arguments when you invoke the script. You can include declarations in the op script or statements in the configuration such that a user executing the script can see the list of possible arguments when using the context-sensitive help in the CLI. The script must also include any necessary declarations and code to process those arguments. The following sections detail how to define the help text and use the arguments in an op script:

Declaring Op Script Command-Line Arguments

There are two ways to define the list of expected op scripts arguments that will be displayed when using context-sensitive help in the CLI:

  • Include declarations in the script

  • Include statements in the Junos OS configuration

Script-generated and configuration-generated arguments have the same operational impact.

To declare arguments within SLAX or XSLT scripts, declare a global variable named arguments, containing <argument> elements. Within each <argument> element, include the required <name> element and an optional <description> element.

XSLT Syntax

SLAX Syntax

To declare arguments within a Python script, declare a global dictionary named arguments containing name-value pairs that map to the argument name and argument description.

Python Syntax

Note

To display the arguments in the CLI, Python scripts must include the if __name__ == '__main__': statement.

Alternatively, you can declare op script arguments in the configuration. To declare arguments in the configuration, include the arguments statement at the [edit system scripts op file filename] hierarchy level.

After you declare an argument in either the script or the configuration, you can use the context-sensitive help to list available arguments. If you include the optional argument description, the text of the description appears in the CLI as a help-text string to describe the purpose of the argument, as discussed in Configuring Help Text for Op Scripts.

user@host> op filename ?

You can create a hidden argument for an op script by not including the argument declarations in the op script or configuration.

Note

If you configure arguments by including the arguments statement in the configuration, any arguments that you declare directly in the script are still available, but are not listed among the Possible completions when you issue the op filename ? command.

If you declare all arguments in the script (and none in the configuration), then the arguments do appear in the Possible completions list. This is because the management (mgd) process populates the Possible completions list by first checking the configuration for arguments. The mgd process looks in the script for arguments only if no arguments are found in the configuration. Thus, if arguments are declared in the configuration, the arguments declared in the script become hidden in the CLI.

Using Command-Line Arguments in Op Scripts

To use command-line arguments in SLAX or XSLT op scripts, you must include a parameter declaration for each argument. The parameter name must be identical to the name provided in the argument declaration. The op script assigns the value for each script argument to the corresponding parameter, which can then be referenced throughout the script.

XSLT Syntax

SLAX Syntax

Python op scripts include the global arguments dictionary to define the arguments that appear in the CLI help for that op script. To process and use the command-line arguments, Python op scripts can use any valid means in the Python language. The following example uses the Python argparse module to process the script arguments. Note that, in this case, the argument names match those in the arguments dictionary.

Python Syntax

When you execute an op script, for each argument you include on the command-line, you must specify a corresponding value. To do this, include an argument-name and an argument-value when you execute the script with the op filename command.

Note

If you specify an argument that the script does not recognize, the script ignores the argument.

Example: Declaring Arguments in XSLT Scripts

Declare two arguments named interface and protocol. Execute the script, specifying the ge-0/2/0.0 interface and the inet protocol as values for the arguments. For either method, you must declare corresponding script parameters.

The following examples show how to declare the arguments in the XSLT script and in the configuration:

Declaring Arguments in the Op Script (script1)

Declaring Arguments in the Configuration

Executing the Script

user@host> op script1 interface ge-0/2/0.0 protocol inet

Example: Declaring and Using Arguments in Python Scripts

Declare two arguments named interface and protocol in the Python op script. Execute the script, specifying the ge-0/2/0.0 interface and the inet protocol as values for the arguments.

Declaring Arguments in the Op Script (script1.py)

Viewing the Arguments

user@host> op script1.py ?

Executing the Script

user@host> op script1.py interface ge-0/2/0.0 protocol inet

Alternatively, instead of including the arguments dictionary in the Python op script, you can include the arguments in the configuration exactly as you would for SLAX and XSLT scripts.