| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import argparse |
| 8 | |
| 9 | from .download import Download |
| 10 | from .model import ModelParser |
| 11 | from .stack import StackParser |
| 12 | |
| 13 | |
| 14 | class LlamaCLIParser: |
| 15 | """Defines CLI parser for Llama CLI""" |
| 16 | |
| 17 | def __init__(self): |
| 18 | self.parser = argparse.ArgumentParser( |
| 19 | prog="llama", |
| 20 | description="Welcome to the Llama CLI", |
| 21 | add_help=True, |
| 22 | ) |
| 23 | |
| 24 | # Default command is to print help |
| 25 | self.parser.set_defaults(func=lambda args: self.parser.print_help()) |
| 26 | |
| 27 | subparsers = self.parser.add_subparsers(title="subcommands") |
| 28 | |
| 29 | # Add sub-commands |
| 30 | Download.create(subparsers) |
| 31 | ModelParser.create(subparsers) |
| 32 | StackParser.create(subparsers) |
| 33 | |
| 34 | def parse_args(self) -> argparse.Namespace: |
| 35 | return self.parser.parse_args() |
| 36 | |
| 37 | def run(self, args: argparse.Namespace) -> None: |
| 38 | args.func(args) |
| 39 | |
| 40 | |
| 41 | def main(): |
| 42 | parser = LlamaCLIParser() |
| 43 | args = parser.parse_args() |
| 44 | parser.run(args) |
| 45 | |
| 46 | |
| 47 | if __name__ == "__main__": |
| 48 | main() |