From 85e2b45d44b0e8287e775d2d32e152124eccb43a Mon Sep 17 00:00:00 2001 From: Alex Sickler Date: Mon, 11 Mar 2024 11:04:41 -0400 Subject: [PATCH 1/3] :sparkles: return default help message when no commands are given --- d3b_dff_cli/cli.py | 64 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/d3b_dff_cli/cli.py b/d3b_dff_cli/cli.py index 7eaa735..01c6bd6 100644 --- a/d3b_dff_cli/cli.py +++ b/d3b_dff_cli/cli.py @@ -1,32 +1,55 @@ import argparse +import sys from .version import __version__ from .modules.validation.check_manifest import main as check_manifest from .modules.validation.check_readgroup import main as check_readgroup from .modules.validation.check_url import main as check_url from .modules.dewrangle.volume import dewrangle_volume + def main(): - parser = argparse.ArgumentParser(description='A command-line interface for d3b-dff-cli.') - subparsers = parser.add_subparsers(title='Available Commands', dest='command') + parser = argparse.ArgumentParser( + description="A command-line interface for d3b-dff-cli." + ) + subparsers = parser.add_subparsers(title="Available Commands", dest="command") # Version - version_parser = subparsers.add_parser("version", help="Display version information.") - version_parser.set_defaults(func=lambda args: print(f"d3b-dff-cli version {__version__}")) + version_parser = subparsers.add_parser( + "version", help="Display version information." + ) + version_parser.set_defaults( + func=lambda args: print(f"d3b-dff-cli version {__version__}") + ) # Validation Command - validation_parser = subparsers.add_parser('validation', help='Validation commands') - validation_subparsers = validation_parser.add_subparsers(title='Validation Subcommands', dest='validation_command') + validation_parser = subparsers.add_parser("validation", help="Validation commands") + validation_subparsers = validation_parser.add_subparsers( + title="Validation Subcommands", dest="validation_command" + ) ## validation manifest subcommand - manifest_parser = validation_subparsers.add_parser('manifest', help='Manifest validation based on defined rules.') - manifest_parser.add_argument('-rules', help='Formatted JSON file defining validation rules.') - manifest_parser.add_argument('-rule_type', help='Specific type of validation rule defined in the json rule file.') - manifest_parser.add_argument('-manifest_file', help='Manifest based on the d3b genomics manifest template.') + manifest_parser = validation_subparsers.add_parser( + "manifest", help="Manifest validation based on defined rules." + ) + manifest_parser.add_argument( + "-rules", help="Formatted JSON file defining validation rules." + ) + manifest_parser.add_argument( + "-rule_type", + help="Specific type of validation rule defined in the json rule file.", + ) + manifest_parser.add_argument( + "-manifest_file", help="Manifest based on the d3b genomics manifest template." + ) manifest_parser.set_defaults(func=check_manifest) ## validation read-group subcommand - parser_bam = validation_subparsers.add_parser("bam", help="Validator for BAM file @RG based on Samtools.") - parser_bam.add_argument("bam_files", nargs="+", help="One or more BAM files to validate #RG") + parser_bam = validation_subparsers.add_parser( + "bam", help="Validator for BAM file @RG based on Samtools." + ) + parser_bam.add_argument( + "bam_files", nargs="+", help="One or more BAM files to validate #RG" + ) parser_bam.set_defaults(func=check_readgroup) ## validation url subcommand @@ -35,17 +58,28 @@ def main(): parser_url.set_defaults(func=check_url) # Dewrangle Command - dewrangle_parser = subparsers.add_parser('dewrangle', help='Dewrangle commands') - dewrangle_subparsers = dewrangle_parser.add_subparsers(title='Dewrangle Subcommands', dest='dewrangle_command') + dewrangle_parser = subparsers.add_parser("dewrangle", help="Dewrangle commands") + dewrangle_subparsers = dewrangle_parser.add_subparsers( + title="Dewrangle Subcommands", dest="dewrangle_command" + ) # dewrangle volume subcommand - dewrangle_volume_parser = dewrangle_subparsers.add_parser("volume", help="Dewrangle volume") + dewrangle_volume_parser = dewrangle_subparsers.add_parser( + "volume", help="Dewrangle volume" + ) dewrangle_volume_parser.set_defaults(func=lambda args: dewrangle_volume()) args = parser.parse_args() + + # if no arguments given, print help message + if len(sys.argv) == 1: + parser.print_help(sys.stderr) + sys.exit(1) + if hasattr(args, "func"): args.func(args) else: print("Invalid command. Use --help for usage information.") + if __name__ == "__main__": main() From 1837f5d5da5f94a0e34c3b535ab9a80791bb6ebc Mon Sep 17 00:00:00 2001 From: Alex Sickler Date: Mon, 11 Mar 2024 11:30:01 -0400 Subject: [PATCH 2/3] :sparkles: print help message when a command is run without arguments --- d3b_dff_cli/cli.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/d3b_dff_cli/cli.py b/d3b_dff_cli/cli.py index 01c6bd6..20c0148 100644 --- a/d3b_dff_cli/cli.py +++ b/d3b_dff_cli/cli.py @@ -78,7 +78,21 @@ def main(): if hasattr(args, "func"): args.func(args) else: - print("Invalid command. Use --help for usage information.") + # something went wrong. Probably a command with no options. Print command's help + # retrieve subparsers from parser + subparsers_actions = [ + action + for action in parser._actions + if isinstance(action, argparse._SubParsersAction) + ] + # loop through subparser actions and find the command we tried to run + for subparsers_action in subparsers_actions: + # get all subparsers and print help + for choice, subparser in subparsers_action.choices.items(): + if choice == args.command: + print("Subparser '{}'".format(choice)) + print(subparser.format_help()) + sys.exit(2) if __name__ == "__main__": From 43b89fc1afa99cba7eeac0e2b41a3cbdc692545b Mon Sep 17 00:00:00 2001 From: Alex Sickler Date: Tue, 12 Mar 2024 09:56:39 -0400 Subject: [PATCH 3/3] :heavy_plus_sign: add required args to manifest validation --- d3b_dff_cli/cli.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/d3b_dff_cli/cli.py b/d3b_dff_cli/cli.py index 20c0148..0ad8a57 100644 --- a/d3b_dff_cli/cli.py +++ b/d3b_dff_cli/cli.py @@ -32,14 +32,17 @@ def main(): "manifest", help="Manifest validation based on defined rules." ) manifest_parser.add_argument( - "-rules", help="Formatted JSON file defining validation rules." + "-rules", help="Formatted JSON file defining validation rules.", required=True ) manifest_parser.add_argument( "-rule_type", help="Specific type of validation rule defined in the json rule file.", + required=True, ) manifest_parser.add_argument( - "-manifest_file", help="Manifest based on the d3b genomics manifest template." + "-manifest_file", + help="Manifest based on the d3b genomics manifest template.", + required=True, ) manifest_parser.set_defaults(func=check_manifest)