Command-line help, closes #4

This commit is contained in:
Lucidiot 2019-10-01 08:26:45 +02:00
parent 36c24c0136
commit 5ece50a3e1
Signed by: lucidiot
GPG Key ID: 3358C1CA6906FB8D
1 changed files with 60 additions and 30 deletions

View File

@ -10,12 +10,25 @@ import click
@click.group(name='twtxt-registry')
@click.argument('registry_url', required=True)
@click.version_option('-V', '--version')
@click.option('-k', '--insecure', is_flag=True)
@click.option('-f', '--format',
type=click.Choice(output.registry.keys()),
default='pretty')
@click.option(
'-k', '--insecure',
is_flag=True,
help='Disable SSL certificate checks.',
)
@click.option(
'-f', '--format',
type=click.Choice(output.registry.keys()),
default='pretty',
help='Change the output format.',
)
@click.pass_context
def cli(ctx, registry_url, insecure, format):
"""
Command-line client for the twtxt registry API.
Takes a mandatory registry URL argument, as the base API URL
(ex. https://registry.twtxt.org/api)
"""
ctx.obj = Namespace()
try:
@ -47,17 +60,18 @@ def cli(ctx, registry_url, insecure, format):
)
@click.pass_context
def register(ctx, nickname, url):
"""
Register a user on a registry.
"""
if not nickname or not url:
try:
config = Config.discover()
except ValueError as e:
if not ctx.obj.conf:
raise click.UsageError(
'Nickname or URL were omitted from the command-line, but they'
'could not be deduced from the twtxt config: {!s}'.format(e),
'could not be deduced from the twtxt config.',
ctx=ctx,
)
nickname = nickname or config.nick
url = url or config.twturl
nickname = nickname or ctx.obj.conf.nick
url = url or ctx.obj.conf.twturl
click.echo(ctx.obj.formatter.format_response(
ctx.obj.client.register(nickname, url, raise_exc=False)
@ -65,9 +79,15 @@ def register(ctx, nickname, url):
@cli.command()
@click.option('-q', '--query')
@click.option(
'-q', '--query',
help='An optional search query to filter users.',
)
@click.pass_context
def users(ctx, query):
"""
List and search users on a registry.
"""
try:
click.echo(ctx.obj.formatter.format_users(
ctx.obj.client.list_users(q=query)
@ -77,9 +97,15 @@ def users(ctx, query):
@cli.command()
@click.option('-q', '--query')
@click.option(
'-q', '--query',
help='An optional search query to filter tweets.',
)
@click.pass_context
def tweets(ctx, query):
"""
List and search tweets on a registry.
"""
try:
click.echo(ctx.obj.formatter.format_tweets(
ctx.obj.client.list_tweets(q=query)
@ -92,28 +118,27 @@ def tweets(ctx, query):
@click.argument('name_or_url', required=False)
@click.pass_context
def mentions(ctx, name_or_url):
"""
List mentions to someone on a registry.
Without arguments, will try to use twtxt's configured URL to list your own
mentions.
"""
if name_or_url:
scheme = urlsplit(name_or_url).scheme
if not scheme: # it could be a nick
try:
config = Config.discover()
except ValueError:
pass
else:
source = config.get_source_by_nick(name_or_url)
if source:
url = source.url
if ctx.obj.conf and not scheme: # it could be a nick
source = ctx.obj.conf.get_source_by_nick(name_or_url)
if source:
url = source.url
url = url or name_or_url # Fallback
elif not ctx.obj.conf:
raise click.UsageError(
'URL was omitted from the command-line, but it could not '
'be deduced from the twtxt config.',
ctx=ctx,
)
else:
try:
config = Config.discover()
except ValueError as e:
raise click.UsageError(
'URL was omitted from the command-line, but it could not '
'be deduced from the twtxt config: {!s}'.format(e),
ctx=ctx,
)
url = config.twturl
url = ctx.obj.conf.twturl
try:
click.echo(ctx.obj.formatter.format_tweets(
@ -127,6 +152,11 @@ def mentions(ctx, name_or_url):
@click.argument('name', required=True)
@click.pass_context
def tag(ctx, name):
"""
Search for tweets containing a tag.
Requires a tag name as a positional argument.
"""
try:
click.echo(ctx.obj.formatter.format_tweets(
ctx.obj.client.list_tag_tweets(name)