Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Declare and Use 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 that enable a user to see the list of possible arguments when they request context-sensitive help for the op script 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 arguments and 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 op script

  • Include statements in the Junos OS configuration

Script-generated and configuration-generated arguments have the same operational impact. The following sections explain how to use the different methods to define the op script arguments and display them in the CLI:

How to Define Arguments in the Op Script

You can declare an op script's expected command-line arguments directly in the Python, SLAX, or XSLT op script.

To declare command-line arguments in Python op scripts:

  1. Declare a global dictionary named arguments.
  2. For each argument, define a name-value pair that maps to the argument name and argument help text.

Python Syntax

Note:

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

To declare command-line arguments in SLAX or XSLT op scripts:

  1. Declare a global variable named arguments.
  2. For each argument, define an <argument> element.
  3. Within each <argument> element:
    • Define the <name> element with the name of the argument.
    • Optionally define a <description> element that provides the help text for that argument.

XSLT Syntax

SLAX Syntax

How to Define Arguments in the Junos OS Configuration

You can declare an op script's expected command-line arguments in the Junos OS configuration, as an alternative to declaring the arguments directly in the op script.

To declare command-line arguments in the configuration:

  1. Navigate to the arguments statement at the [edit system scripts op file filename] hierarchy level for the given script.
  2. Configure the argument name.
  3. Optionally configure the description statement to provide the help text for the argument.

For example:

How to Display Arguments in the Context-Sensitive Help

After you declare arguments in either the op script or the configuration, you can use the CLI's context-sensitive help to list the op script arguments. If you include the optional argument description, the CLI displays the help text with the argument name.

You can also create a hidden argument for an op script by not including the argument declaration in the op script or the configuration. You use the argument as you normally would in the script, but the CLI does not display the argument or help text when you request context-sensitive help for that op script.

Note:

If you configure command-line arguments in the Junos OS configuration and also declare arguments directly in the op script, the arguments that you declare in the script are still available, but the CLI does not list them under Possible completions when you issue the op filename ? command. This occurs because the management (mgd) process populates the list by first checking the configuration for arguments. The mgd process checks the script for arguments only if no arguments are found in the configuration. Thus, if you declare arguments in the configuration, any arguments declared in the script become hidden in the CLI.

For more information about configuring help text for op scripts, see Configure Help Text for Op Scripts.

Using Command-Line Arguments in Op Scripts

You execute local op scripts with the op filename command. To pass command-line arguments to the script, include each argument name and value when you execute the script.

Note:

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

The following sections discuss how to use the command-line arguments that are passed to Python, SLAX, and XSLT op scripts:

How to Use Arguments in Python Op Scripts

Python op scripts can use standard command-line parsing libraries to process and use command-line arguments. For example, you can use the Python argparse library to easily define required and optional arguments, specify default values, and handle the arguments in the script.

To enable users to more easily use the standard Python libraries to parse command-line arguments, we modified the way that the arguments are passed to Python op scripts. Starting in Junos OS Release 21.2R1 and Junos OS Evolved Release 21.2R1, when the device passes command-line arguments to a Python op script, it prefixes a single hyphen (-) to single-character argument names and prefixes two hyphens (--) to multi-character argument names. In earlier releases, the devices prefixes a single hyphen (-) to all argument names. You should ensure that your op script properly handles the arguments for your specific release.

The following examples use the argparse module to handle the script arguments. The examples define the global arguments dictionary, and the dictionary keys are used to define the expected arguments for the parser. We provide two example scripts, which appropriately handle the arguments in the specified releases.

Python Syntax (Junos OS Release 21.2R1 or later)

Python Syntax (Junos OS Release 21.1 and earlier)

How to Use Arguments in SLAX and XSLT Op Scripts

To use command-line arguments in SLAX or XSLT op scripts, you must:

  1. Include a parameter declaration for each argument
  2. Ensure the parameter name is identical to the name that you defined in either the arguments variable declaration in the script or the arguments statement in the Junos OS configuration.

XSLT Syntax

SLAX Syntax

The op script assigns the value for each script argument to the corresponding parameter, which can then be referenced throughout the script.

Example: Declaring Arguments in XSLT Op 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.

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

Declaring Arguments in the Op Script (script1)

Declaring Arguments in the Configuration

In addition to declaring the arguments in the script or the configuration, you must also declare the corresponding parameters in the script in order to reference the script arguments and access their values.

Declaring the Parameters

Provide the argument names and values when you execute the script. For example:

Executing the Script

Example: Declaring and Using Arguments in Python Op Scripts

Declare two arguments named interface and p 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. Select the appropriate argument handling statements based on your release. The script uses statements compatible with Junos OS Release 21.2R1 and later and comments out the statements for handling arguments in older releases.

Declaring Arguments in the Op Script (script1.py)

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.

To view the op script arguments in the CLI's context-sensitive help, issue the op filename ? command.

Displaying the Arguments

Provide the argument names and values when you execute the script. For example:

Executing the Script

Change History Table

Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.

Release
Description
21.2R1 and 21.2R1-EVO
Starting in Junos OS Release 21.2R1 and Junos OS Evolved Release 21.2R1, when the device passes command-line arguments to a Python op script, it prefixes a single hyphen (-) to single-character argument names and prefixes two hyphens (--) to multi-character argument names.