-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New cli: added root cmd parameters & sub-commands
- Loading branch information
Showing
2 changed files
with
148 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,171 @@ | ||
"""New command line interface for voc4cat with subcommands""" | ||
"""New cleaner command line interface for voc4cat with subcommands.""" | ||
|
||
import argparse | ||
import logging | ||
import sys | ||
from pathlib import Path | ||
|
||
from voc4cat import setup_logging | ||
from voc4cat.checks import Voc4catError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def transform(args): | ||
print("Transform subcommand executed!") | ||
|
||
|
||
def convert(args): | ||
print("Convert subcommand executed!") | ||
|
||
|
||
def check(args): | ||
print("Check subcommand executed!") | ||
|
||
|
||
def docs(args): | ||
print("Docs subcommand executed!") | ||
|
||
|
||
def add_transform_subparser(subparsers): | ||
# transform Conversions with xlsx files as input and output | ||
# file-to-process | ||
# --make-ids prefix start-ID Specify prefix to search and replace by ID-based vocabulary IRIs. ... | ||
# --hierarchy-from-indent Convert concept sheet with indentation to children-URI hierarchy. | ||
# --hierarchy-to-indent Convert concept sheet from children-URI hierarchy to indentation. | ||
# --indent-separator Separator character(s) to read/write indented hierarchies (default: xlsx indent) | ||
# -O --outputdir here or better on subcmds? | ||
# --force (was: --no-warn) here or better on subcmds? | ||
parser = subparsers.add_parser("transform", help="Transform data") | ||
parser.add_argument("transformers", nargs="*", help="pick as many as you want") | ||
parser.set_defaults(command="transform") | ||
|
||
|
||
def add_convert_subparser(subparsers): | ||
# convert Convert between rdf and xlsx and back. | ||
# file-to-process | ||
# --rdf-format xml, turtle, json-ld Output format of generated rdf. | ||
# --output-type screen/file Where to output generated rdf. | ||
# --template Path to xlsx template to be used when converting to xlsx. | ||
# -O --outputdir here or better on subcmds? | ||
|
||
parser = subparsers.add_parser("convert", help="Convert data") | ||
parser.set_defaults(command="convert") | ||
|
||
|
||
def add_check_subparser(subparsers): | ||
# check Validation and checks | ||
# file-to-process | ||
# --xlsx | ||
# --ci | ||
# --shacl | ||
# --listprofiles | ||
# -p --profile Profile name or file (implies --shacl = True) | ||
# --force (was: --no-warn) here or better on subcmds? | ||
|
||
parser = subparsers.add_parser("check", help="Check something") | ||
parser.set_defaults(command="check") | ||
|
||
|
||
def add_docs_subparser(subparsers): | ||
# docs Documentation generation from rdf | ||
# file-to-process | ||
# -O --outputdir here or better on subcmds? | ||
parser = subparsers.add_parser("docs", help="Generate documentation") | ||
parser.add_argument( | ||
"--config", help="Path to configuration file for special settings" | ||
) | ||
parser.add_argument( | ||
"verse", nargs="*", help="Optionally pick as many verses as you want load" | ||
) | ||
parser.set_defaults(command="docs") | ||
|
||
|
||
def main_cli(args=None): | ||
""" | ||
_summary_ | ||
_extended_summary_ | ||
Parameters | ||
---------- | ||
args : _type_, optional | ||
_description_, by default None | ||
Possible command structure: | ||
voc4cat | ||
<subcommands> | ||
-V --version | ||
-H --help | ||
-v -vv --verbose | ||
-q -qq --quiet | ||
-l --logfile | ||
--force (was: --no-warn) here or better on subcmds? | ||
-O --outputdir here or better on subcmds? | ||
transform Conversions with xlsx files as input and output | ||
file-to-process | ||
--make-ids prefix start-ID Specify prefix to search and replace by ID-based vocabulary IRIs. ... | ||
--hierarchy-from-indent Convert concept sheet with indentation to children-URI hierarchy. | ||
--hierarchy-to-indent Convert concept sheet from children-URI hierarchy to indentation. | ||
--indent-separator Separator character(s) to read/write indented hierarchies (default: xlsx indent) | ||
convert Convert between rdf and xlsx and back. | ||
file-to-process | ||
--rdf-format xml, turtle, json-ld Output format of generated rdf. | ||
--output-type screen/file Where to output generated rdf. | ||
--template Path to xlsx template to be used when converting to xlsx. | ||
docs Documentation generation from rdf | ||
file-to-process | ||
check Validation and checks | ||
file-to-process | ||
--xlsx | ||
--ci | ||
--shacl | ||
--listprofiles | ||
-p --profile Profile name or file (implies --shacl = True) | ||
""" | ||
parser = argparse.ArgumentParser( | ||
prog="voc4cat-ng", formatter_class=argparse.ArgumentDefaultsHelpFormatter | ||
prog="voc4cat-ng", | ||
description="Next generation of command line interface for voc4cat-tool.", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
parser.add_argument( | ||
"-V", | ||
"--version", | ||
help="The version of voc4cat-tool.", | ||
action="store_true", | ||
) | ||
parser.add_argument( | ||
"-v", | ||
"--verbose", | ||
action="count", | ||
dest="verboser", | ||
default=0, | ||
help="Make output more verbose. Repeat to increase verbosity (-vv or -vvv).", | ||
) | ||
parser.add_argument( | ||
"-q", | ||
"--quiet", | ||
action="count", | ||
default=0, | ||
dest="quieter", | ||
help="Make output less verbose. Repeat to decrease verbosity (-qq or -qqq).", | ||
) | ||
parser.add_argument( | ||
"-l", | ||
"--logfile", | ||
help=( | ||
"Activate logging to a file at given path. " | ||
"The path will be created if it is not existing." | ||
), | ||
type=Path, | ||
required=False, | ||
) | ||
|
||
# subparsers = parser.add_subparsers(help="sub-command help") | ||
subparsers = parser.add_subparsers( | ||
title="Subcommands", | ||
dest="subcommand", | ||
description="Get help for commands with voc4cat-ng COMMAND --help", | ||
) | ||
|
||
def start_cli_app(args=None): | ||
add_transform_subparser(subparsers) | ||
add_convert_subparser(subparsers) | ||
add_check_subparser(subparsers) | ||
add_docs_subparser(subparsers) | ||
|
||
if not args: | ||
parser.print_help() | ||
|
||
# Parse the command-line arguments | ||
# Note, pars_args will sys.exit with 2 if invalid commands are given. | ||
args = parser.parse_args() | ||
|
||
# Call the appropriate function based on the selected subcommand | ||
if args.subcommand == "transform": | ||
transform(args) | ||
elif args.subcommand == "convert": | ||
convert(args) | ||
elif args.subcommand == "docs": | ||
docs(args) | ||
elif args.subcommand == "check": | ||
check(args) | ||
|
||
|
||
def run_cli_app(args=None): | ||
if args is None: # app started via entrypoint | ||
args = sys.argv[1:] | ||
|
||
setup_logging() | ||
|
||
try: | ||
main_cli(args) | ||
except Voc4catError: | ||
logger.exception() | ||
logger.exception("Terminating with Voc4cat error.") | ||
sys.exit(1) | ||
except Exception: | ||
logger.exception() | ||
logger.exception("Unhandled / unexpected error.") | ||
sys.exit(2) | ||
|
||
|
||
if __name__ == "__main__": | ||
start_cli_app(sys.argv[1:]) | ||
run_cli_app(sys.argv[1:]) |