Use betterhelp to manage docstrings, aliases, and helptexts.

master
Ethan Dalool 2020-01-30 23:25:24 -08:00
parent a151edd549
commit 5345e8f4fc
1 changed files with 36 additions and 51 deletions

View File

@ -1,6 +1,8 @@
import argparse import argparse
import sys import sys
from voussoirkit import betterhelp
from . import exceptions from . import exceptions
# NOTE: Originally I wanted the docstring for each module to be within their # NOTE: Originally I wanted the docstring for each module to be within their
@ -22,26 +24,32 @@ The basics:
3. Stay up-to-date 3. Stay up-to-date
> timesearch.py livestream -r subredditname > timesearch.py livestream -r subredditname
Commands for collecting: Commands for collecting:
{get_submissions} {get_submissions}
{get_comments} {get_comments}
{livestream} {livestream}
{get_styles} {get_styles}
{get_wiki} {get_wiki}
Commands for processing: Commands for processing:
{breakdown} {breakdown}
{index} {index}
{merge_db} {merge_db}
{offline_reading} {offline_reading}
TO SEE DETAILS ON EACH COMMAND, RUN TO SEE DETAILS ON EACH COMMAND, RUN
> timesearch.py <command> > timesearch.py <command>
''' '''.lstrip()
MODULE_DOCSTRINGS = { MODULE_DOCSTRINGS = dict(
'breakdown': ''' breakdown='''
breakdown: breakdown:
Give the comment / submission counts for users in a subreddit, or Give the comment / submission counts for users in a subreddit, or
the subreddits that a user posts to. the subreddits that a user posts to.
@ -61,9 +69,9 @@ breakdown:
--sort "name" | "submissions" | "comments" | "total_posts" --sort "name" | "submissions" | "comments" | "total_posts"
Sort the output. Sort the output.
''', '''.strip(),
'get_comments': ''' get_comments='''
get_comments: get_comments:
Collect comments on a subreddit or comments made by a user. Collect comments on a subreddit or comments made by a user.
@ -77,6 +85,10 @@ get_comments:
-l "update" | --lower "update": -l "update" | --lower "update":
If a number - the unix timestamp to start at. If a number - the unix timestamp to start at.
If "update" - continue from latest comment in db. If "update" - continue from latest comment in db.
WARNING: If at some point you collected comments for a particular
submission which was ahead of the rest of your comments, using "update"
will start from that later submission, and you will miss the stuff in
between that specific post and the past.
Default: update Default: update
-up 1467460221 | --upper 1467460221: -up 1467460221 | --upper 1467460221:
@ -90,16 +102,16 @@ get_comments:
-v | --verbose: -v | --verbose:
If provided, print extra information to the screen. If provided, print extra information to the screen.
''', '''.strip(),
'get_styles': ''' get_styles='''
get_styles: get_styles:
Collect the stylesheet, and css images. Collect the stylesheet, and css images.
> timesearch.py get_styles -r subredditname > timesearch.py get_styles -r subredditname
''', '''.strip(),
'get_submissions': ''' get_submissions='''
get_submissions: get_submissions:
Collect submissions from the subreddit across all of history, or Collect submissions from the subreddit across all of history, or
Collect submissions by a user (as many as possible). Collect submissions by a user (as many as possible).
@ -129,16 +141,16 @@ get_submissions:
-v | --verbose: -v | --verbose:
If provided, print extra information to the screen. If provided, print extra information to the screen.
''', '''.strip(),
'get_wiki': ''' get_wiki='''
get_wiki: get_wiki:
Collect all available wiki pages. Collect all available wiki pages.
> timesearch.py get_wiki -r subredditname > timesearch.py get_wiki -r subredditname
''', '''.strip(),
'index': ''' index='''
index: index:
Dump submission listings to a plaintext or HTML file. Dump submission listings to a plaintext or HTML file.
@ -200,9 +212,9 @@ index:
`timesearch index -r botwatch --all` `timesearch index -r botwatch --all`
performs all of the different mashes. performs all of the different mashes.
''', '''.strip(),
'livestream': ''' livestream='''
livestream: livestream:
Continously collect submissions and/or comments. Continously collect submissions and/or comments.
@ -233,9 +245,9 @@ livestream:
-1 | --once: -1 | --once:
If provided, only do a single loop. Otherwise go forever. If provided, only do a single loop. Otherwise go forever.
''', '''.strip(),
'merge_db': ''' merge_db='''
merge_db: merge_db:
Copy all new posts from one timesearch database into another. Copy all new posts from one timesearch database into another.
@ -249,9 +261,9 @@ merge_db:
The database file to which you will copy the posts. The database file to which you will copy the posts.
The database is modified in-place. The database is modified in-place.
Existing posts will be ignored and not updated. Existing posts will be ignored and not updated.
''', '''.strip(),
'offline_reading': ''' offline_reading='''
offline_reading: offline_reading:
Render submissions and comment threads to HTML via Markdown. Render submissions and comment threads to HTML via Markdown.
@ -262,18 +274,10 @@ offline_reading:
-s "t3_xxxxxx" | --specific "t3_xxxxxx": -s "t3_xxxxxx" | --specific "t3_xxxxxx":
Given a submission ID, t3_xxxxxx, render only that submission. Given a submission ID, t3_xxxxxx, render only that submission.
Otherwise render every submission in the database. Otherwise render every submission in the database.
''', '''.strip(),
} )
OLD_COMMAND_ALIASES = {
'timesearch': 'get_submissions',
'commentaugment': 'get_comments',
'getstyles': 'get_styles',
'getwiki': 'get_wiki',
'mergedb': 'merge_db',
'redmash': 'index',
}
DOCSTRING = betterhelp.add_previews(DOCSTRING, MODULE_DOCSTRINGS)
def docstring_preview(text): def docstring_preview(text):
''' '''
@ -412,27 +416,8 @@ p_get_submissions.add_argument('-v', '--verbose', dest='verbose', action='store_
p_get_submissions.add_argument('--dont_supplement', dest='do_supplement', action='store_false') p_get_submissions.add_argument('--dont_supplement', dest='do_supplement', action='store_false')
p_get_submissions.set_defaults(func=get_submissions_gateway) p_get_submissions.set_defaults(func=get_submissions_gateway)
@betterhelp.subparser_betterhelp(parser, main_docstring=DOCSTRING, sub_docstrings=MODULE_DOCSTRINGS)
def main(argv): def main(argv):
helpstrings = {'', 'help', '-h', '--help'}
command = listget(argv, 0, '').lower()
command = OLD_COMMAND_ALIASES.get(command, command)
# The user did not enter a command, or entered something unrecognized.
if command not in MODULE_DOCSTRINGS:
print(DOCSTRING)
if command == '':
print('You are seeing the default help text because you did not choose a command.')
elif command not in helpstrings:
print('You are seeing the default help text because "%s" was not recognized' % command)
return 1
# The user entered a command, but no further arguments, or just help.
argument = listget(argv, 1, '').lower()
if argument in helpstrings:
print(MODULE_DOCSTRINGS[command])
return 1
args = parser.parse_args(argv) args = parser.parse_args(argv)
try: try:
args.func(args) args.func(args)