Skip to content

Commit 385a512

Browse files
authored
Improve cli documenation (#5818)
* refactor: create `get_parser()` * test * markdown renderer for argparse commands * enhance cli documentation * enable CI workflows * generate controls overview and link stylesheet * apply review suggestions * add flet-cli in pyproject.toml * improve render_controls_overview
1 parent 151309d commit 385a512

File tree

25 files changed

+718
-793
lines changed

25 files changed

+718
-793
lines changed

sdk/python/packages/flet-cli/src/flet_cli/cli.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ def set_default_subparser(
6060
args.insert(index, name)
6161

6262

63-
def main():
63+
def get_parser() -> argparse.ArgumentParser:
64+
"""Construct and return the CLI argument parser."""
6465
parser = argparse.ArgumentParser()
66+
67+
# add version flag
6568
parser.add_argument(
6669
"--version",
6770
"-V",
@@ -71,19 +74,24 @@ def main():
7174

7275
sp = parser.add_subparsers(dest="command")
7376

77+
# register subcommands
7478
flet_cli.commands.create.Command.register_to(sp, "create")
7579
flet_cli.commands.run.Command.register_to(sp, "run")
7680
flet_cli.commands.build.Command.register_to(sp, "build")
7781
flet_cli.commands.pack.Command.register_to(sp, "pack")
7882
flet_cli.commands.publish.Command.register_to(sp, "publish")
7983
flet_cli.commands.serve.Command.register_to(sp, "serve")
80-
flet_cli.commands.doctor.Command.register_to(
81-
sp, "doctor"
82-
) # Register the doctor command
84+
flet_cli.commands.doctor.Command.register_to(sp, "doctor")
8385

8486
# set "run" as the default subparser
8587
set_default_subparser(parser, name="run", index=1)
8688

89+
return parser
90+
91+
92+
def main():
93+
parser = get_parser()
94+
8795
# print usage/help if called without arguments
8896
if len(sys.argv) == 1:
8997
parser.print_help(sys.stdout)

sdk/python/packages/flet-cli/src/flet_cli/commands/base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import argparse
2-
from typing import Any, List, Optional
2+
from typing import Any, Optional
33

44
from flet_cli.commands.options import Option, verbose_option
55

@@ -13,7 +13,7 @@ class BaseCommand:
1313
description: Optional[str] = None
1414
# A list of pre-defined options which will be loaded on initializing
1515
# Rewrite this if you don't want the default ones
16-
arguments: List[Option] = [verbose_option]
16+
arguments: list[Option] = [verbose_option]
1717

1818
def __init__(self, parser: argparse.ArgumentParser) -> None:
1919
for arg in self.arguments:
@@ -25,14 +25,15 @@ def register_to(
2525
cls,
2626
subparsers: argparse._SubParsersAction,
2727
name: Optional[str] = None,
28-
**kwargs: Any
28+
**kwargs: Any,
2929
) -> None:
3030
"""Register a subcommand to the subparsers,
3131
with an optional name of the subcommand.
3232
"""
3333
help_text = cls.description or cls.__doc__
3434
name = name or cls.name or ""
35-
# Remove the existing subparser as it will raises an error on Python 3.11+
35+
36+
# Remove the existing subparser as it will raise an error on Python 3.11+
3637
subparsers._name_parser_map.pop(name, None)
3738
subactions = subparsers._get_subactions()
3839
subactions[:] = [action for action in subactions if action.dest != name]

0 commit comments

Comments
 (0)