databased.customshell

 1import argparse
 2
 3from argshell import ArgShellParser, Namespace, with_parser
 4from pathier import Pathier
 5
 6from databased import Databased, dbparsers
 7from databased.dbshell import DBShell
 8
 9
10class CustomShell(DBShell):
11    _dbpath: Pathier = None  # Replace None with a path to a database file to set a default database # type: ignore
12    connection_timeout: float = 10
13    detect_types: bool = True
14    enforce_foreign_keys: bool = True
15    intro = "Starting customshell (enter help or ? for command info)..."
16    prompt = "customshell>"
17
18
19# For help with adding custom functionality see:
20# https://github.com/matt-manes/argshell
21# https://github.com/matt-manes/databased/blob/main/src/databased/dbshell.py
22# https://github.com/matt-manes/databased/blob/main/src/databased/dbparsers.py
23
24
25def get_args() -> argparse.Namespace:
26    parser = argparse.ArgumentParser()
27
28    parser.add_argument(
29        "dbpath",
30        nargs="?",
31        type=str,
32        help=""" The database file to use. If not provided the current working directory will be scanned for database files. """,
33    )
34    args = parser.parse_args()
35
36    return args
37
38
39def main(args: argparse.Namespace | None = None):
40    if not args:
41        args = get_args()
42    dbshell = CustomShell()
43    if args.dbpath:
44        dbshell.dbpath = Pathier(args.dbpath)
45    dbshell.cmdloop()
46
47
48if __name__ == "__main__":
49    main(get_args())
class CustomShell(databased.dbshell.DBShell):
11class CustomShell(DBShell):
12    _dbpath: Pathier = None  # Replace None with a path to a database file to set a default database # type: ignore
13    connection_timeout: float = 10
14    detect_types: bool = True
15    enforce_foreign_keys: bool = True
16    intro = "Starting customshell (enter help or ? for command info)..."
17    prompt = "customshell>"

Subclass this to create custom ArgShells.

def get_args() -> argparse.Namespace:
26def get_args() -> argparse.Namespace:
27    parser = argparse.ArgumentParser()
28
29    parser.add_argument(
30        "dbpath",
31        nargs="?",
32        type=str,
33        help=""" The database file to use. If not provided the current working directory will be scanned for database files. """,
34    )
35    args = parser.parse_args()
36
37    return args
def main(args: argparse.Namespace | None = None):
40def main(args: argparse.Namespace | None = None):
41    if not args:
42        args = get_args()
43    dbshell = CustomShell()
44    if args.dbpath:
45        dbshell.dbpath = Pathier(args.dbpath)
46    dbshell.cmdloop()