Skip to content

Commit

Permalink
multiple apps: use argparse to support standard -h help
Browse files Browse the repository at this point in the history
- group-lines
- qgifview
- password-prompt
- tailsleep
  • Loading branch information
hydrargyrum committed Feb 26, 2023
1 parent cda707a commit da0c43c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
8 changes: 6 additions & 2 deletions group-lines/group-lines
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: WTFPL

import argparse
import re
import sys


pattern = re.compile(sys.argv[1])
parser = argparse.ArgumentParser()
parser.add_argument("pattern")
args = parser.parse_args()

pattern = re.compile(args.pattern)

groups = {}

Expand Down
11 changes: 6 additions & 5 deletions password-prompt/password-prompt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

# reads a password on TTY (not stdin) and show it on stdout

import argparse
import getpass
import sys


try:
prompt = f"{sys.argv[1].rstrip()} "
except IndexError:
prompt = "Password: "
parser = argparse.ArgumentParser()
parser.add_argument("prompt", default="Password:", nargs="?")
args = parser.parse_args()

prompt = f"{args.prompt.rstrip()} "

print(getpass.getpass(prompt))
2 changes: 1 addition & 1 deletion qgifview/qgifview
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from PyQt5.QtWidgets import QApplication, QLabel
app = QApplication(sys.argv)

args = app.arguments()
if len(args) != 2:
if len(args) != 2 or sys.argv[1] in {'-h', '--help'}:
print('usage: %s FILE.GIF' % args[0], file=sys.stderr)
sys.exit(1)

Expand Down
8 changes: 4 additions & 4 deletions tailsleep/tailsleep
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ def read_file(filename,
sleep_time=1, end_after_inactivity_iter=5,
suggested_block_size=4096):
"""Generates data of a growing file.
Yields blocks of data.
When file end is reached, it waits `sleep_time` seconds
`end_after_inactivity_iter` times. If file has no more data within
that period, the generator ends.
"""

def _read_some():
for _ in range(end_after_inactivity_iter):
block = f.read(suggested_block_size)
if block:
return block
time.sleep(sleep_time)

with open(filename, 'rb') as f:
while True:
block = _read_some()
Expand All @@ -42,7 +42,7 @@ def _quit(signum, stack):
def main():
locale.setlocale(locale.LC_ALL, '')

if len(sys.argv) != 2:
if len(sys.argv) != 2 or sys.argv[1] in {'-h', '--help'}:
print('usage: %s FILE' % sys.argv[0], file=sys.stderr)
sys.exit(os.EX_USAGE)

Expand Down

0 comments on commit da0c43c

Please sign in to comment.