-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonScriptTemplate.py
More file actions
executable file
·118 lines (90 loc) · 2.87 KB
/
Copy pathpythonScriptTemplate.py
File metadata and controls
executable file
·118 lines (90 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ==============================================================================
# pythonScriptTemplate.py
# ==============================================================================
# Template to create new python cli scripts.
# Author: Evan Harmon
# Usage: python script.py --input input.txt --output output.txt --verbose
import argparse
import logging
import sys
__version__ = "1.0.0"
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
def parse_args():
"""
Parse command line arguments.
"""
parser = argparse.ArgumentParser(
description="A Python CLI script",
epilog="Example: python script.py --input file.txt",
)
# Define arguments
parser.add_argument(
"-i", "--input", type=str, required=True, help="Path to the input file"
)
parser.add_argument(
"-o",
"--output",
type=str,
default="output.txt",
help="Path to the output file (default: output.txt)",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose mode"
)
parser.add_argument(
"--version",
action="version",
version=f"%(prog)s {__version__}",
help="Show the script version and exit",
)
return parser.parse_args()
def validate_args(args):
"""
Validate command line arguments.
"""
# Example validation: check if the input file exists
if not args.input.endswith(".txt"):
logger.error("Invalid input file. Must be a .txt file.")
sys.exit(1)
logger.info(f"Using input file: {args.input}")
logger.info(f"Output will be saved to: {args.output}")
def process_file(input_path, output_path):
"""
Placeholder for the main file processing logic.
"""
try:
with open(input_path, "r") as infile:
content = infile.read()
# Example processing: just write the content to the output file
with open(output_path, "w") as outfile:
outfile.write(content)
logger.info(f"File processed successfully. Output saved to {output_path}")
except FileNotFoundError:
logger.error(f"File {input_path} not found.")
sys.exit(1)
except Exception as e:
logger.error(f"An error occurred: {str(e)}")
sys.exit(1)
def main():
"""
Main entry point of the script.
"""
args = parse_args()
# Enable verbose mode if requested
if args.verbose:
logger.setLevel(logging.DEBUG)
logger.debug("Arguments parsed successfully.")
# Validate the arguments
validate_args(args)
# Process the file
process_file(args.input, args.output)
if __name__ == "__main__":
main()