Skip to content

Commit 08bfecc

Browse files
feat: Add the ability to read from stdin
1 parent dcf7236 commit 08bfecc

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cowsay/__main__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import sys
23

34
from . import CowsayError, char_names, char_funcs, __version__
45

@@ -15,7 +16,7 @@ def cli():
1516
default='cow')
1617

1718
parser.add_argument('-t', '--text',
18-
required=True)
19+
help='Text to display. If not provided, reads from stdin.')
1920

2021
parser.add_argument('-v', '--version',
2122
action='version', version=__version__)
@@ -25,7 +26,14 @@ def cli():
2526
if args.character not in char_names:
2627
raise CowsayError(f'Available Characters: {char_names}')
2728

28-
char_funcs[args.character](args.text)
29+
if args.text:
30+
text = args.text
31+
else:
32+
if sys.stdin.isatty():
33+
parser.error('Text argument is required if not piping input.')
34+
text = sys.stdin.read().strip()
35+
36+
char_funcs[args.character](text)
2937

3038

3139
if __name__ == "__main__":

0 commit comments

Comments
 (0)