From 6f81fd178d81a5d24a78f7ec20b011de542289c9 Mon Sep 17 00:00:00 2001 From: poka Date: Sat, 5 Dec 2020 01:34:30 +0100 Subject: [PATCH] Big improve on args parser --- jaklis.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/jaklis.py b/jaklis.py index 72d3072..f12468d 100755 --- a/jaklis.py +++ b/jaklis.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import argparse, sys, os, random, string, getpass +import argparse, sys, os, random, string, getpass, json from os.path import join, dirname from shutil import copyfile from dotenv import load_dotenv @@ -22,7 +22,7 @@ parser.add_argument('-v', '--version', action='store_true', help="Affiche la ver parser.add_argument('-k', '--key', help="Chemin vers mon trousseau de clé (PubSec)") parser.add_argument('-n', '--node', help="Adresse du noeud Cesium+ ou Gchange à utiliser") -subparsers = parser.add_subparsers() +subparsers = parser.add_subparsers(dest="cmd") read_cmd = subparsers.add_parser('read', help="Lecture des messages") send_cmd = subparsers.add_parser('send', help="Envoi d'un message") delete_cmd = subparsers.add_parser('delete', help="Supression d'un message") @@ -32,19 +32,6 @@ eraseProfile_cmd = subparsers.add_parser('erase', help="Effacer son profile Cesi like_cmd = subparsers.add_parser('like', help="Voir les likes d'un profile / Liker un profile (option -s NOTE)") unlike_cmd = subparsers.add_parser('unlike', help="Supprimer un like") -if sys.argv[1] in ('-k','-n','--key','--node'): - if sys.argv[3] in ('-k','-n','--key','--node'): - cmd = sys.argv[5] - else: - cmd = sys.argv[3] -else: - cmd = sys.argv[1] - -if len(sys.argv) <= 1 or not cmd in ('read','send','delete','set','get','erase','like','unlike','-v','--version'): - sys.stderr.write("Veuillez indiquer une commande valide:\n\n") - parser.print_help() - sys.exit(1) - # Messages management read_cmd.add_argument('-n', '--number',type=int, default=3, help="Affiche les NUMBER derniers messages") read_cmd.add_argument('-j', '--json', action='store_true', help="Sort au format JSON") @@ -77,6 +64,7 @@ like_cmd.add_argument('-s', '--stars', type=int, help="Nombre d'étoile") unlike_cmd.add_argument('-p', '--profile', help="Profile à déliker") args = parser.parse_args() +cmd = args.cmd if args.version: print(VERSION)