diff --git a/src/prodir/__init__.py b/src/prodir/__init__.py index 520a304..defd188 100644 --- a/src/prodir/__init__.py +++ b/src/prodir/__init__.py @@ -1 +1,13 @@ -from prodir._tree_structurer import create_tree_structurer, get_structure \ No newline at end of file +from prodir._tree_structurer import ( + create_tree_structurer, + get_structure, + create_structure_from_file, + create_structure_from_string +) + +__all__ = [ + 'create_tree_structurer', + 'get_structure', + 'create_structure_from_file', + 'create_structure_from_string' +] \ No newline at end of file diff --git a/src/prodir/__main__.py b/src/prodir/__main__.py index 1967b82..ecb4aa6 100644 --- a/src/prodir/__main__.py +++ b/src/prodir/__main__.py @@ -1,36 +1,120 @@ import os import sys import argparse -from prodir import create_tree_structurer, get_structure +from prodir import ( + create_tree_structurer, + get_structure, + create_structure_from_file, + create_structure_from_string +) def main(): - parser = argparse.ArgumentParser(description='Display directory structure in a tree-like format') - parser.add_argument('path', nargs='?', default=os.getcwd(), - help='Path to analyze (default: current directory)') - parser.add_argument('-v', '--verbose', action='store_true', - help='Show more detailed output') + parser = argparse.ArgumentParser( + description='Directory structure tool: Display and create directory structures' + ) + + # Create subparsers for different commands + subparsers = parser.add_subparsers(dest='command', help='Available commands') + + # Display command + display_parser = subparsers.add_parser('display', help='Display directory structure') + display_parser.add_argument( + 'path', + nargs='?', + default=os.getcwd(), + help='Path to analyze (default: current directory)' + ) + display_parser.add_argument( + '-v', '--verbose', + action='store_true', + help='Show more detailed output' + ) + + # Create command + create_parser = subparsers.add_parser('create', help='Create directory structure') + create_parser.add_argument( + '-f', '--file', + help='File containing the directory structure' + ) + create_parser.add_argument( + '-s', '--string', + help='String containing the directory structure' + ) + create_parser.add_argument( + '-o', '--output', + default=os.getcwd(), + help='Output directory (default: current directory)' + ) + create_parser.add_argument( + '-v', '--verbose', + action='store_true', + help='Show more detailed output' + ) + args = parser.parse_args() - + + # If no command is specified, default to display + if not args.command: + args.command = 'display' + args.path = os.getcwd() + args.verbose = False + try: - if args.verbose: - print(f"Analyzing directory: {args.path}") - create_tree_structurer() - structure = get_structure(args.path) - - for line in structure: - print(line) + + if args.command == 'display': + if args.verbose: + print(f"Analyzing directory: {args.path}") + structure = get_structure(args.path) + for line in structure: + print(line) + + elif args.command == 'create': + if not args.file and not args.string: + parser.error("Either --file or --string must be specified") + + if args.file and args.string: + parser.error("Cannot specify both --file and --string") + + if args.verbose: + print(f"Creating directory structure in: {args.output}") + + if args.file: + if args.verbose: + print(f"Using structure from file: {args.file}") + create_structure_from_file(args.file, args.output) + + elif args.string: + if args.verbose: + print("Creating structure from provided string") + create_structure_from_string(args.string, args.output) + + if args.verbose: + print("Structure created successfully") + print("\nResulting structure:") + structure = get_structure(args.output) + for line in structure: + print(line) + except Exception as e: error_msg = str(e) if "Directory does not exist" in error_msg: - print(f"Error: The specified directory does not exist: {args.path}", file=sys.stderr) + print(f"Error: The specified directory does not exist: {args.path if args.command == 'display' else args.output}", + file=sys.stderr) elif "Directory is empty" in error_msg: - print(f"Error: The specified directory is empty: {args.path}", file=sys.stderr) + print(f"Error: The specified directory is empty: {args.path if args.command == 'display' else args.output}", + file=sys.stderr) elif "Path is not a directory" in error_msg: - print(f"Error: The specified path is not a directory: {args.path}", file=sys.stderr) + print(f"Error: The specified path is not a directory: {args.path if args.command == 'display' else args.output}", + file=sys.stderr) + elif "Unable to open structure file" in error_msg: + print(f"Error: Unable to open structure file: {args.file}", file=sys.stderr) + elif "Invalid structure format" in error_msg: + print(f"Error: Invalid structure format in {'file' if args.file else 'string'}", file=sys.stderr) else: print(f"Error: {error_msg}", file=sys.stderr) sys.exit(1) + if __name__ == "__main__": main() \ No newline at end of file