Skip to main content

FastAPI CLI Command

Project description

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 single-command

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/single-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('single-command')
class CommandCommand(BaseCommand):
  def __init__(self):
    pass

  async def run(self, foo: CommandOptions.REQUIRED_OPTION, bar: CommandOptions.PROMPT_OPTION = None):
    ...
    
  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.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 GroupCommandCommand:
  def __init__(self):
    pass

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

  @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
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 SeederGroupCommand:
  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 = None):
    ...

  @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
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 = None):
    user = await self.get_user_by_email(email)
    await self.update_username_by_email(user.id, username)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_cli_command-0.0.3.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

fastapi_cli_command-0.0.3-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_cli_command-0.0.3.tar.gz.

File metadata

  • Download URL: fastapi_cli_command-0.0.3.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for fastapi_cli_command-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8edffea62048d7f2975eb7aec89a1472de33ff0e9cdcc537f906895dbfae6369
MD5 9df63e5323ec3a5d17d38b6fa366cfee
BLAKE2b-256 2f248caef614d54f0240046ef44d4b7e591c77c236caefea9a45243fd9f38639

See more details on using hashes here.

File details

Details for the file fastapi_cli_command-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_cli_command-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f0f66a5235e759bc682d215cdfe5a886b3d55eaa4aaa0ca2a35bdf15dbf633fe
MD5 854d3c2221c80ffab89a1597c071283f
BLAKE2b-256 e9da2152163867cbcbb6477900aa97c0ca0615de4084d4a8da1682739c51219f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page