From 3f4d051b8493569794899a761dfd1363debcc1d9 Mon Sep 17 00:00:00 2001 From: Hartmut Goebel Date: Fri, 29 Mar 2019 20:58:19 +0100 Subject: [PATCH] Add minimal argparse support to get support for `--help`. Closes #5. --- feediverse.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/feediverse.py b/feediverse.py index 5a51207..b9645b9 100755 --- a/feediverse.py +++ b/feediverse.py @@ -2,6 +2,7 @@ import os import sys +import argparse import yaml import dateutil import feedparser @@ -9,8 +10,17 @@ import feedparser from mastodon import Mastodon from datetime import datetime, timezone +DEFAULT_CONFIG_FILE = os.path.join("~", ".feediverse") + def main(): - config_file = get_config_file() + parser = argparse.ArgumentParser() + parser.add_argument("config_file", nargs="?", metavar="CONFIG-FILE", + help=("config file to use, default: %s" % + DEFAULT_CONFIG_FILE), + default=os.path.expanduser(DEFAULT_CONFIG_FILE)) + args = parser.parse_args() + config_file = args.config_file + if not os.path.isfile(config_file): setup(config_file) @@ -29,13 +39,6 @@ def main(): save_config(config, config_file) -def get_config_file(): - if __name__ == "__main__" and len(sys.argv) > 1: - config_file = sys.argv[1] - else: - config_file = os.path.join(os.path.expanduser("~"), ".feediverse") - return config_file - def save_config(config, config_file): copy = dict(config) copy['updated'] = datetime.now(tz=timezone.utc).isoformat()