DocsIntegratingIntegrationsArgparse

Fig Autocomplete & argparse

This tutorial will walk you through setting up Fig's integration with Python's built in argument parsing framework, argparse.

Install the integration

Add the argparse-complete-fig package to your project's dependencies using pip:

pip install argparse-complete-fig

Setup the integration

Setup the integration with argparse by importing and calling the add_completion_spec_command function on your top-level parser.

import argparse
from argparse_complete_fig import add_completion_spec_command

parser = argparse.ArgumentParser(prog='my-cli')

# Call on your root-level parser for full completions
add_completion_spec_command(parser)

This function will add a --generate-fig-spec argument to the parser. When your CLI is invoked with this flag it will print a Fig spec.

Generate the spec

You can now automatically generate the completion spec skeleton by running the following command:

my-cli --generate-fig-spec

To test the completions in dev mode, you can run

# Create dev environment.
yarn create completion-spec my-cli

# Use generated fig spec.
my-cli --generate-fig-spec > .fig/autocomplete/src/my-cli.ts

# Launch autocomplete dev mode.
cd .fig/autocomplete
yarn dev

If you edit your app and want to test the changes, just regenerate the spec in the .fig/autocomplete/src/ folder while dev mode is running.

Next Steps: now that you have successfully configured the integration you can either push the spec to the autocomplete repo or push it to Fig's cloud.