From bc4c514cbbe91a074f3032d16ac28fee94c8d483 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sat, 14 Jan 2023 08:55:58 +0100 Subject: [PATCH] added argparse class by Steven Bethard --- src/brewserverblocklist/bargparse.py | 27 +++++++++++++++++++ .../brewserverblocklist.py | 4 +-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/brewserverblocklist/bargparse.py diff --git a/src/brewserverblocklist/bargparse.py b/src/brewserverblocklist/bargparse.py new file mode 100644 index 0000000..a04d9dd --- /dev/null +++ b/src/brewserverblocklist/bargparse.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +# SPDX-FileCopyrightText: 2023 Tobias Diekershoff +# +# SPDX-License-Identifier: CC-BY-SA-2.5 + +""" +This expansion of the ArgParser class will display the --help results by +default if an error occurs (e.g. no arguments are passed to the script). + +It is based on an StackOverflow answer from 2010 by unutbu who refered to +a reply from Steven Bethard as source of the code. Postings of that time +on StackOverflow are published under the terms of a CreativeCommons license +specifically under the CC-BY-SA-2.5 so this module is put under the same +license. + +https://stackoverflow.com/questions/4042452/display-help-message-with-python-argparse-when-script-is-called-without-any-argu/4042861#4042861 +""" + +import argparse +import sys + +class MyParser(argparse.ArgumentParser): + def error(self, message): + sys.stderr.write('error: %s\n' % message) + self.print_help() + sys.exit(2) diff --git a/src/brewserverblocklist/brewserverblocklist.py b/src/brewserverblocklist/brewserverblocklist.py index 04154dd..ca901ce 100644 --- a/src/brewserverblocklist/brewserverblocklist.py +++ b/src/brewserverblocklist/brewserverblocklist.py @@ -8,11 +8,11 @@ of any number of other Friendica instances. See https://git.friendi.ca/tobias/brewserverblocklist """ -import argparse import configparser import sys from os.path import exists import requests +import bargparse class BrewBlocklist(): """ @@ -157,7 +157,7 @@ def main(): * collect the ingredient * serve the result """ - parser = argparse.ArgumentParser() + parser = bargparse.MyParser() parser.add_argument('-c', '--config', dest='configfile', required=True,