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    commit_on_close: bool = True
16    log_dir: Pathier = Pathier(__file__).parent
17    intro = "Starting customshell (enter help or ? for command info)..."
18    prompt = "customshell>"
19
20
21# For help with adding custom functionality see:
22# https://github.com/matt-manes/argshell
23# https://github.com/matt-manes/databased/blob/main/src/databased/dbshell.py
24# https://github.com/matt-manes/databased/blob/main/src/databased/dbparsers.py
25
26
27def get_args() -> argparse.Namespace:
28    parser = argparse.ArgumentParser()
29
30    parser.add_argument(
31        "dbpath",
32        nargs="?",
33        type=str,
34        help=""" The database file to use. If not provided the current working directory will be scanned for database files. """,
35    )
36    args = parser.parse_args()
37
38    return args
39
40
41def main(args: argparse.Namespace | None = None):
42    if not args:
43        args = get_args()
44    dbshell = CustomShell()
45    if args.dbpath:
46        dbshell.dbpath = Pathier(args.dbpath)
47    dbshell.cmdloop()
48
49
50if __name__ == "__main__":
51    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    commit_on_close: bool = True
17    log_dir: Pathier = Pathier(__file__).parent
18    intro = "Starting customshell (enter help or ? for command info)..."
19    prompt = "customshell>"

Subclass this to create custom ArgShells.

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