Skip to content

Commit

Permalink
Add subcommands to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jan 3, 2019
1 parent a17eb92 commit 93821cc
Showing 1 changed file with 64 additions and 48 deletions.
112 changes: 64 additions & 48 deletions src/aria2p/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import argparse
import json
import sys
from datetime import timedelta

from .api import API
from .client import JSONRPCClient, JSONRPCError
Expand All @@ -39,72 +38,89 @@ def get_method(name, default=None):
def get_parser():
"""Return a parser for the command-line options and arguments."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="subcommand")

mutually_exclusive = parser.add_mutually_exclusive_group()
call_parser = subparsers.add_parser("call", help="Call a remote method through the JSON-RPC client.")

rpc_group = mutually_exclusive.add_argument_group()
rpc_group.add_argument("-m", "--method", dest="method")
call_parser.add_argument("method")

rpc_params_group = rpc_group.add_mutually_exclusive_group()
rpc_params_group.add_argument("-p", "--params", dest="params", nargs="+")
rpc_params_group.add_argument("-j", "--json-params", dest="json_params")
call_parser_mxg = call_parser.add_mutually_exclusive_group()
call_parser_mxg.add_argument("-p", "--params", dest="params", nargs="+")
call_parser_mxg.add_argument("-j", "--json-params", dest="json_params")

add_magnet_parser = subparsers.add_parser("add-magnet", help="Add a download with a Magnet URI.")
add_magnet_parser.add_argument("uri")

add_torrent_parser = subparsers.add_parser("add-torrent", help="Add a download with a Torrent file.")
add_torrent_parser.add_argument("torrent_file")

# general_group = mutually_exclusive.add_argument_group()
# sub-commands: list, add, pause, resume, stop, remove, search, info
# use click?

return parser


def main(args=None):
"""The main function, which is executed when you type ``aria2p`` or ``python -m aria2p``."""
client = JSONRPCClient()
api = API(client)

parser = get_parser()
args = parser.parse_args(args=args)

if args.method:
method = get_method(args.method)
params = []
if args.params:
params = args.params
elif args.json_params:
params = json.loads(args.json_params)
if method is None:
print(f"Unknown method {args.method}. Run '{sys.argv[0]} -m listmethods' to list the available methods.")
return 1
try:
try:

if args.subcommand == "call":

method = get_method(args.method)
if method is None:
print(
f"Unknown method {args.method}. Run '{sys.argv[0]} -m listmethods' to list the available methods."
)
return 1

params = []
if args.params:
params = args.params
elif args.json_params:
params = json.loads(args.json_params)

response = client.call(method, params)
except JSONRPCError as e:
print(e.message)
return e.code
else:
print(json.dumps(response))
return 0

api = API(client)
return 0

try:
downloads = api.get_downloads()
except Exception as e:
print(e)
return 1

print(f"{'GID':<17} "
f"{'STATUS':<9} "
f"{'PROGRESS':>8} "
f"{'DOWN_SPEED':>12} "
f"{'UP_SPEED':>12} "
f"{'ETA':>8} "
f"NAME")

for download in downloads:
print(f"{download.gid:<17} "
f"{download.status:<9} "
f"{download.progress_string():>8} "
f"{download.download_speed_string():>12} "
f"{download.upload_speed_string():>12} "
f"{download.eta_string():>8} "
f"{download.name}")
elif args.subcommand == "add-magnet":
api.add_magnet(args.uri)

elif args.subcommand == "add-torrent":
api.add_torrent(args.torrent_file)

else:
downloads = api.get_downloads()

print(
f"{'GID':<17} "
f"{'STATUS':<9} "
f"{'PROGRESS':>8} "
f"{'DOWN_SPEED':>12} "
f"{'UP_SPEED':>12} "
f"{'ETA':>8} "
f"NAME"
)

for download in downloads:
print(
f"{download.gid:<17} "
f"{download.status:<9} "
f"{download.progress_string():>8} "
f"{download.download_speed_string():>12} "
f"{download.upload_speed_string():>12} "
f"{download.eta_string():>8} "
f"{download.name}"
)

except JSONRPCError as e:
print(e.message)
return e.code

return 0

0 comments on commit 93821cc

Please sign in to comment.