Skip to main content

FastAPI CLI Command

FastAPI CLI Command package helps you:

  • Automatically generate CLI commands.
  • Construct command modules in a clean and organized manner.
  • Automatically inject dependencies into your commands.

By utilizing the click package behind the scenes, the commands can leverage features such as command arguments, options, command groups, and more.

Dependencies

  • fastapi
  • click

Installation

pip install fastapi_cli_command

Command generation

  1. Generate single command
fastapi_cli_command generate command --path {path}

Example: To generate single command, run:

fastapi_cli_command generate command --path src/users/commands --name test

Note: You can omit the --name option from the CLI command, as click will prompt you for input.

This will create a file at src/users/commands/single-command.py with the following content:

# src/users/commands/test_command.py
from click import Option
from fastapi_cli_command import BaseCommand, command


class CommandOptions:
  REQUIRED_OPTION = Option(
    ['--foo'],
    help='Description for option',
    required=True,
    type=str
  )
  PROMPT_OPTION = Option(
    ['--bar'],
    help='Description for option',
    prompt=True,
    type=int,
    default=99
  )

@command('test')
class TestCommand(BaseCommand):
  def __init__(self):
    pass

  async def run(self, foo: CommandOptions.REQUIRED_OPTION, bar: CommandOptions.PROMPT_OPTION):
    ...
    
  1. Generate group command
fastapi_cli_command generate group-command --path={path}

Example: To generate a group seeder command, run:

fastapi_cli_command generate group-command --path src/seed/commands --name seeder
# src/seed/commands/seeder_command.py
from click import Option
from fastapi_cli_command import cli_command, cli_command_group

class CommandOptions:
  REQUIRED_OPTION = Option(
    ['--foo'],
    help='Description for option',
    required=True,
    type=str
  )
  PROMPT_OPTION = Option(
    ['--bar'],
    help='Description for option',
    prompt=True,
    type=int,
    default=99
  )

@cli_command_group('seeder')
class SeederGroupCommand:
  def __init__(self):
    pass

  @cli_command('REPLACE_ME')
  async def command(self, foo: CommandOptions.REQUIRED_OPTION, bar: CommandOptions.PROMPT_OPTION):
    ...

  @cli_command('REPLACE_ME')
  async def command_2(self):
    ...
    

When to use?

Scenario 1: Seeding Data into the Database

If you want to seed user and post data into the database, you can create a group command:

fastapi_cli_command generate group-command --path src/seed/commands --name seed
from click import Option
from fastapi_cli_command import cli_command, cli_command_group

class CommandOptions:
  USER_QUANTITY = Option(
    ['--quantity'],
    help='How many users that you want to generate',
    required=True,
    prompt=True,
    type=int
  )
  POST_QUANTITY = Option(
    ['--quantity'],
    help='How many users that you want to generate',
    type=int,
    default=99
  )

@cli_command_group('seed')
class SeedGroupCommand:
  def __init__(self, user_repo: UserRepository = Depends(), post_repo: PostRepository = Depends()):
    self.user_repo = user_repo
    self.post_repo = post_repo

  @cli_command('user')
  async def seed_user(self, quantity: CommandOptions.USER_QUANTITY):
    ...

  @cli_command('post')
  async def seed_post(self, quantity: CommandOptions.POST_QUANTITY = None):
    ...
    

Then, you can run the following commands:

fastapi_cli_command seed user
fastapi_cli_command seed post --quantity 10

Scenario 2: Updating a User's Username

If you want to update a user's username using their email address, you can create a single command:

fastapi_cli_command generate command --path src/users/commands --name update-username
from click import Option
from fastapi_cli_command import BaseCommand, command

class CommandOptions:
  EMAIL = Option(
    ['--email'],
    help='Email address of user that you want to update',
    prompt=True,
    required=True,
    type=str,
  )
  USERNAME = Option(
    ['--username'],
    help='Username that you want to replace',
    required=True,
    prompt=True,
    type=str,
  )

@command('update-username')
class UpdateUsernameCommand(BaseCommand):
  def __init__(self, user_repo: UserRepository = Depends()):
    self.user_repo = user_repo

  async def get_user_by_email(self, email: str):
    ...

  async def update_username_by_email(self, user_id: UUID, username: str):
    ...

  async def run(self, email: TestCommandOptions.EMAIL, username: TestCommandOptions.USERNAME):
    user = await self.get_user_by_email(email)
    await self.update_username_by_email(user.id, username)

Metadata

Release files for fastapi-cli-command 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-cli-command 0.0.6
File Size Uploaded
fastapi_cli_command-0.0.6.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-cli-command 0.0.6
File Interpreter ABI Platform
fastapi_cli_command-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / fastapi_cli_command-0.0.6.tar.gz

Download URL fastapi_cli_command-0.0.6.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c8503d06012a0f0a1c7ac6b4439bc251f13d277e47821464d048065f58004209
BLAKE2b-256 checksum
How to use checksums
d47f6c39361d390140eb954614df186816de64cdd79601658fde6c95d8d28895
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.14

Release files / fastapi_cli_command-0.0.6-py3-none-any.whl

Download URL fastapi_cli_command-0.0.6-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee4c79b2b9069441891823691b5da8e741ac0b44035fd0d04d33f8dc2e4eab3a
BLAKE2b-256 checksum
How to use checksums
c72702521786a2c765db6c464a8c6c744ca0ddcd283562cf37456a39f463dae5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.14

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page