[PYTHON] Use click to create a sub-sub command --netsted sub-sub command -

Overview

When creating a command line tool using a command line analysis tool in python, the modules used are roughly divided into the following three.

Since argparse is a standard library, it has the advantage that it can be used without thinking, and since click is not a standard module, it is easy to implement things that are difficult to implement in the standard library.

Since I have never used fire and this article is not intended to introduce command line analysis tools, I will omit further comparison and explanation here.

How to create a subcommand using click

The method of creating a subcommand using click is as follows.

If you want to create a program that actually works, it will be as follows.

import click

@click.group()
def cli():
    pass

@cli.command()
def initdb():
    click.echo('Initialized the database')

@cli.command()
def dropdb():
    click.echo('Dropped the database')

def main():
    cli()


if __name__ == "__main__":
    main()
$ python3 cli.py 
Usage: cli.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  dropdb
  initdb
$ python3 cli.py dropdb
Dropped the database
$ python3 cli.py dropdb --help
Usage: cli.py dropdb [OPTIONS]

Options:
  --help  Show this message and exit.
$ python3 cli.py initdb --help
Usage: cli.py initdb [OPTIONS]

Options:
  --help  Show this message and exit.
$ python3 cli.py initdb
Initialized the database

How to make sub-sub commands

The above is how to make it when program command <options> Recently, the number of kubectl etc. that receive two subcommands such as kubectl get pods <options> is increasing. Therefore, what should I do if I make it myself?

Even if I read the click document, it says how to make subcommands, but not how to make subsubcommands. I searched around some documents and the Web and thought that the following method was beautiful, so I made it.

.
├── modules
│   ├── sub1.py
│   └── sub2.py
└── cli.py
import click
from modules import sub1
from modules import sub2

@click.group()
def cli():
    pass

def main():
    cli.add_command(sub1.sub1cli)
    cli.add_command(sub2.sub2cli)
    cli()


if __name__ == "__main__":
    main()
import click

@click.group()
def sub1cli():
    pass

@sub1cli.command()
def sub1cmd1():
    click.echo('sub1cmd1')

@sub1cli.command()
def sub1cmd2():
    click.echo('sub1cmd2')
import click

@click.group()
def sub2cli():
    pass

@sub2cli.command()
def sub2cmd1():
    click.echo('sub2cmd1')

@sub2cli.command()
def sub2cmd2():
    click.echo('sub2cmd2')

In this way, you can create it by adding click.group with click's add_command.

$ python3 cli.py 
Usage: cli.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  sub1cli
  sub2cli
$ python3 cli.py sub1cli
Usage: cli.py sub1cli [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  sub1cmd1
  sub1cmd2

$ python3 cli.py sub2cli
Usage: cli.py sub2cli [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  sub2cmd1
  sub2cmd2

Recommended Posts

Use click to create a sub-sub command --netsted sub-sub command -
Create a command to encode / decode Splunk base64
Try to create a new command on linux
How to create a shortcut command for LINUX
Create a command to get the work log
Command to create Linux Live USB
Steps to create a Django project
How to create a Conda package
How to use MBDyn (command setting)
How to create a virtual bridge
How to create a Dockerfile (basic)
5 Ways to Create a Python Chatbot
How to create a config file
Create a command line tool to convert dollars to yen using Python
I tried to create a linebot (implementation)
How to create a clone from Github
Create a bot to retweet coronavirus information
Make SikuliX's click function easier to use
How to create a git clone folder
I tried to create a linebot (preparation)
[python] How to use __command__, function explanation
[Linux] How to use the echo command
How to calculate Use% of df command
Create a command to delete all temporary files generated in a specific folder
Various ways to create a dictionary (memories)
How to use CUT command (with sample)
How to create a repository from media
Don't use rm command to delete files
Script to create a Mac dictionary file
3 best ways to use the less command
Zip-compress any file with the [shell] command to create a file and delete the original file.
[Python] List Comprehension Various ways to create a list
[Python] How to test command line parser click
A simple example of how to use ArgumentParser
Add a command to mark similar files together
I want to easily create a Noise Model
How to create a Python virtual environment (venv)
How to create a function object from a string
I want to create a window in Python
Randomly sample MNIST data to create a dataset
How to create a JSON file in Python
If you want to create a Word Cloud.
Create a custom search command in Splunk (Streaming Command)
Steps to create a Twitter bot with python
I want to create a plug-in type implementation
[Note] How to create a Ruby development environment
How to create a Kivy 1-line input box
How to create a multi-platform app with kivy
How to create a Rest Api in Django
Create a Python console application easily with Click
Use a cool graph to analyze PES data!
[Note] How to create a Mac development environment
It is better to use NTFS when connecting SSD to Linux to create a file server.
Use Twitter API to reduce the time taken by Twitter (create a highlighting (like) timeline)
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
Read the Python-Markdown source: How to create a parser
A memo to create a virtual environment (venv) before Django
How to create an article from the command line
[For beginners] How to use say command in python!
I tried to create a table only with Django
Create a function to visualize / evaluate the clustering result