File: plot_7_sys_argv.py

package info (click to toggle)
sphinx-gallery 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,336 kB
  • sloc: python: 10,346; makefile: 216; lisp: 15; sh: 11; cpp: 9
file content (22 lines) | stat: -rw-r--r-- 795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
Using ``sys.argv`` in examples
==============================

This example demonstrates the use of ``sys.argv`` in example ``.py`` files.

By default, all example ``.py`` files will be run by Sphinx-Gallery **without** any
arguments. Notice below that ``sys.argv`` is a list consisting of only the
file name. Further, any arguments added will take on the default value.

This behavior can be changed by using the `reset_argv` option in the sphinx configuration, see :ref:`reset_argv`.

"""  # noqa: E501

import argparse
import sys

parser = argparse.ArgumentParser(description="Toy parser")
parser.add_argument("--option", default="default", help="a dummy optional argument")
sys.argv[0] = sys.argv[0].split("/")[-1]
print("sys.argv:", sys.argv)
print("parsed args:", parser.parse_args())