Expand file tree
/
Copy pathhowler.py
More file actions
82 lines (64 loc) · 2.46 KB
/
Copy pathhowler.py
File metadata and controls
82 lines (64 loc) · 2.46 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
#!/usr/bin/env python3
"""Howler"""
import argparse
import os
import sys
# --------------------------------------------------
def get_args():
"""
Parse command-line arguments for the Howler program.
This function sets up the argument parser and defines the expected
command-line arguments. It handles both direct text input and file input,
reading the contents of the file if a valid file path is provided.
Returns:
argparse.Namespace: An object containing the parsed arguments:
- text (str): The input text to be converted to uppercase.
If a file path is provided, this will contain
the file's contents.
- outfile (str): The name of the output file. If not provided,
the program will use standard output.
Raises:
SystemExit: If invalid arguments are provided.
"""
parser = argparse.ArgumentParser(
description='Howler (upper-case input)',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('text',
metavar='text',
type=str,
help='Input string or file')
parser.add_argument('-o',
'--outfile',
help='Output filename',
metavar='str',
type=str,
default='')
args = parser.parse_args()
if os.path.isfile(args.text):
with open(args.text) as in_fh:
args.text = in_fh.read().rstrip()
return args
# --------------------------------------------------
def main():
"""
Execute the main functionality of the Howler program.
This function performs the following steps:
1. Retrieves command-line arguments.
2. Opens the output file (or uses stdout if no file is specified).
3. Converts the input text to uppercase.
4. Writes the uppercase text to the output.
5. Closes the output file handle if one was opened.
The function doesn't take any parameters as it uses command-line arguments
parsed by the get_args() function.
Returns:
None
"""
args = get_args()
if args.outfile:
with open(args.outfile, 'w') as out_fh:
out_fh.write(args.text.upper() + '\n')
else:
sys.stdout.write(args.text.upper() + '\n')
# --------------------------------------------------
if __name__ == '__main__':
main()