|
1 | 1 | """Main entry point for the application.""" |
2 | 2 |
|
| 3 | +############################################################################## |
| 4 | +# Python imports. |
| 5 | +from argparse import ArgumentParser, Namespace |
| 6 | +from inspect import cleandoc |
| 7 | +from operator import attrgetter |
| 8 | + |
| 9 | +############################################################################## |
| 10 | +# Textual enhanced imports. |
| 11 | +from textual_enhanced.commands import Command |
| 12 | + |
3 | 13 | ############################################################################## |
4 | 14 | # Local imports. |
| 15 | +from . import __doc__, __version__ |
5 | 16 | from .app import Braindrop |
6 | 17 | from .app.data import ExitState |
7 | 18 |
|
8 | 19 |
|
| 20 | +############################################################################## |
| 21 | +def get_args() -> Namespace: |
| 22 | + """Get the command line arguments. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + The arguments. |
| 26 | + """ |
| 27 | + |
| 28 | + # Build the parser. |
| 29 | + parser = ArgumentParser( |
| 30 | + prog="braindrop", |
| 31 | + description=__doc__, |
| 32 | + epilog=f"v{__version__}", |
| 33 | + ) |
| 34 | + |
| 35 | + # Add --version |
| 36 | + parser.add_argument( |
| 37 | + "-v", |
| 38 | + "--version", |
| 39 | + help="Show version information", |
| 40 | + action="version", |
| 41 | + version=f"%(prog)s v{__version__}", |
| 42 | + ) |
| 43 | + |
| 44 | + # Add --license |
| 45 | + parser.add_argument( |
| 46 | + "--license", |
| 47 | + "--licence", |
| 48 | + help="Show license information", |
| 49 | + action="store_true", |
| 50 | + ) |
| 51 | + |
| 52 | + # Add --bindings |
| 53 | + parser.add_argument( |
| 54 | + "-b", |
| 55 | + "--bindings", |
| 56 | + help="List commands that can have their bindings changed", |
| 57 | + action="store_true", |
| 58 | + ) |
| 59 | + |
| 60 | + # Finally, parse the command line. |
| 61 | + return parser.parse_args() |
| 62 | + |
| 63 | + |
| 64 | +############################################################################## |
| 65 | +def show_bindable_commands() -> None: |
| 66 | + """Show the commands that can have bindings applied.""" |
| 67 | + from rich.console import Console |
| 68 | + from rich.markup import escape |
| 69 | + |
| 70 | + from .app.screens import Main |
| 71 | + |
| 72 | + console = Console(highlight=False) |
| 73 | + command: type[Command] |
| 74 | + for command in sorted(Main.COMMAND_MESSAGES, key=attrgetter("__name__")): |
| 75 | + if command().has_binding: |
| 76 | + console.print( |
| 77 | + f"[bold]{escape(command.__name__)}[/] [dim italic]- {escape(command.tooltip())}[/]" |
| 78 | + ) |
| 79 | + console.print( |
| 80 | + f" [dim italic]Default: {escape(command.binding().key)}[/]" |
| 81 | + ) |
| 82 | + |
| 83 | + |
9 | 84 | ############################################################################## |
10 | 85 | def main() -> None: |
11 | 86 | """Main entry point.""" |
12 | | - match Braindrop().run(): |
13 | | - case ExitState.TOKEN_FORGOTTEN: |
14 | | - if Braindrop.environmental_token(): |
15 | | - print( |
16 | | - "It looks like your token is held in an environment variable. " |
17 | | - "If you wish to have that forgotten you will need to remove it yourself." |
18 | | - ) |
19 | | - else: |
20 | | - print("The locally-held copy of your API token has been removed.") |
21 | | - case ExitState.TOKEN_NEEDED: |
22 | | - print("An API token is needed to be able to connect to raindrop.io.") |
| 87 | + args = get_args() |
| 88 | + if args.license: |
| 89 | + print(cleandoc(Braindrop.HELP_LICENSE)) |
| 90 | + elif args.bindings: |
| 91 | + show_bindable_commands() |
| 92 | + else: |
| 93 | + match Braindrop().run(): |
| 94 | + case ExitState.TOKEN_FORGOTTEN: |
| 95 | + if Braindrop.environmental_token(): |
| 96 | + print( |
| 97 | + "It looks like your token is held in an environment variable. " |
| 98 | + "If you wish to have that forgotten you will need to remove it yourself." |
| 99 | + ) |
| 100 | + else: |
| 101 | + print("The locally-held copy of your API token has been removed.") |
| 102 | + case ExitState.TOKEN_NEEDED: |
| 103 | + print("An API token is needed to be able to connect to raindrop.io.") |
23 | 104 |
|
24 | 105 |
|
25 | 106 | ############################################################################## |
|
0 commit comments