Deprecated -l flag, converted URL to positional argument

This commit is contained in:
Michael Lazar
2017-08-30 22:04:48 -04:00
parent 41333da82e
commit 91b35fe7fd
3 changed files with 34 additions and 10 deletions

View File

@@ -32,13 +32,14 @@ def build_parser():
epilog=docs.CONTROLS, epilog=docs.CONTROLS,
formatter_class=argparse.RawDescriptionHelpFormatter) formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument( parser.add_argument(
'-V', '--version', action='version', version='rtv '+__version__) 'link', metavar='URL', nargs='?',
help='[optional] Full URL of a submission to open')
parser.add_argument( parser.add_argument(
'-s', dest='subreddit', '-s', dest='subreddit',
help='Name of the subreddit that will be opened on start') help='Name of the subreddit that will be loaded on start')
parser.add_argument( parser.add_argument(
'-l', dest='link', '-l', dest='link_deprecated',
help='Full URL of a submission that will be opened on start') help=argparse.SUPPRESS) # Deprecated, use the positional arg instead
parser.add_argument( parser.add_argument(
'--log', metavar='FILE', action='store', '--log', metavar='FILE', action='store',
help='Log HTTP requests to the given file') help='Log HTTP requests to the given file')
@@ -52,8 +53,7 @@ def build_parser():
'--monochrome', action='store_const', const=True, '--monochrome', action='store_const', const=True,
help='Disable color') help='Disable color')
parser.add_argument( parser.add_argument(
'--non-persistent', dest='persistent', action='store_const', '--non-persistent', dest='persistent', action='store_const', const=False,
const=False,
help='Forget the authenticated user when the program exits') help='Forget the authenticated user when the program exits')
parser.add_argument( parser.add_argument(
'--clear-auth', dest='clear_auth', action='store_const', const=True, '--clear-auth', dest='clear_auth', action='store_const', const=True,
@@ -67,6 +67,8 @@ def build_parser():
parser.add_argument( parser.add_argument(
'--enable-media', dest='enable_media', action='store_const', const=True, '--enable-media', dest='enable_media', action='store_const', const=True,
help='Open external links using programs defined in the mailcap config') help='Open external links using programs defined in the mailcap config')
parser.add_argument(
'-V', '--version', action='version', version='rtv '+__version__)
return parser return parser
@@ -206,6 +208,12 @@ class Config(object):
parser = build_parser() parser = build_parser()
args = vars(parser.parse_args()) args = vars(parser.parse_args())
# Overwrite the deprecated "-l" option into the link variable
if args['link_deprecated'] and args['link'] is None:
args['link'] = args['link_deprecated']
args.pop('link_deprecated', None)
# Filter out argument values that weren't supplied # Filter out argument values that weren't supplied
return {key: val for key, val in args.items() if val is not None} return {key: val for key, val in args.items() if val is not None}

View File

@@ -7,12 +7,11 @@ desktop:https://github.com/michael-lazar/rtv:{version}\
""" """
SUMMARY = """ SUMMARY = """
Reddit Terminal Viewer is a lightweight browser for www.reddit.com built into a Reddit Terminal Viewer is a lightweight browser for https://www.reddit.com
terminal window.
""" """
CONTROLS = """ CONTROLS = """
Move the cursor using either the arrow keys or *Vim* style movement. Move the cursor using the arrow keys or vim style movement.
Press `?` to open the help screen. Press `?` to open the help screen.
""" """

View File

@@ -80,8 +80,8 @@ def test_config_get_args():
"Ensure that command line arguments are parsed properly" "Ensure that command line arguments are parsed properly"
args = ['rtv', args = ['rtv',
'https://reddit.com/permalink •',
'-s', 'cfb', '-s', 'cfb',
'-l', 'https://reddit.com/permalink •',
'--log', 'logfile.log', '--log', 'logfile.log',
'--config', 'configfile.cfg', '--config', 'configfile.cfg',
'--ascii', '--ascii',
@@ -113,6 +113,23 @@ def test_config_get_args():
assert config['enable_media'] is True assert config['enable_media'] is True
def test_config_link_deprecated():
# Should still be able to specify the link using the old "-l"
args = ['rtv', '-l', 'https://reddit.com/option']
with mock.patch('sys.argv', args):
config_dict = Config.get_args()
config = Config(**config_dict)
assert config['link'] == 'https://reddit.com/option'
# But the positional argument should take preference
args = ['rtv', 'https://reddit.com/arg', '-l', 'https://reddit.com/option']
with mock.patch('sys.argv', args):
config_dict = Config.get_args()
config = Config(**config_dict)
assert config['link'] == 'https://reddit.com/arg'
def test_config_from_file(): def test_config_from_file():
"Ensure that config file arguments are parsed properly" "Ensure that config file arguments are parsed properly"