diff --git a/src/cli.py b/src/cli.py index 54c4a19..715bb17 100755 --- a/src/cli.py +++ b/src/cli.py @@ -36,15 +36,15 @@ parser = argparse.ArgumentParser( # Add an argument for the command parser.add_argument( "command", - help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove, snapshot, test or version.", + help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove, snapshot, set, test or version.", metavar="command", - choices=["add", "clear", "config", "disable", "list", "remove", "snapshot", "test", "version"]) + choices=["add", "clear", "config", "disable", "list", "remove", "set", "snapshot", "test", "version"]) # Add an argument for the extra arguments of diable and remove parser.add_argument( - "argument", - help="Either 0 (enable) or 1 (disable) for the disable command, or the model ID for the remove command.", - nargs="?") + "arguments", + help="Optional arguments for the add, disable, remove and set commands.", + nargs="*") # Add the user flag parser.add_argument( @@ -109,6 +109,8 @@ elif args.command == "list": import cli.list elif args.command == "remove": import cli.remove +elif args.command == "set": + import cli.set elif args.command == "snapshot": import cli.snap elif args.command == "test": diff --git a/src/cli/add.py b/src/cli/add.py index d1739cf..7aa7e01 100644 --- a/src/cli/add.py +++ b/src/cli/add.py @@ -76,8 +76,12 @@ if not builtins.howdy_args.plain: # Set the default label label = "Initial model" +# Get the label from the cli arguments if provided +if builtins.howdy_args.arguments: + label = builtins.howdy_args.arguments[0] + # If models already exist, set that default label -if encodings: +elif encodings: label = "Model #" + str(len(encodings) + 1) # Keep de default name if we can't ask questions diff --git a/src/cli/disable.py b/src/cli/disable.py index 971015d..7fb9d8a 100644 --- a/src/cli/disable.py +++ b/src/cli/disable.py @@ -15,18 +15,21 @@ config = configparser.ConfigParser() config.read(config_path) # Check if enough arguments have been passed -if builtins.howdy_args.argument is None: +if not builtins.howdy_args.arguments: print("Please add a 0 (enable) or a 1 (disable) as an argument") sys.exit(1) +# Get the cli argument +argument = builtins.howdy_args.arguments[0] + # Translate the argument to the right string -if builtins.howdy_args.argument == "1" or builtins.howdy_args.argument.lower() == "true": +if argument == "1" or argument.lower() == "true": out_value = "true" -elif builtins.howdy_args.argument == "0" or builtins.howdy_args.argument.lower() == "false": +elif argument == "0" or argument.lower() == "false": out_value = "false" else: # Of it's not a 0 or a 1, it's invalid - print("Please only use false (enable) or true (disable) as an argument") + print("Please only use 0 (enable) or 1 (disable) as an argument") sys.exit(1) # Don't do anything when the state is already the requested one diff --git a/src/cli/remove.py b/src/cli/remove.py index b41c9bf..e4f4736 100644 --- a/src/cli/remove.py +++ b/src/cli/remove.py @@ -11,7 +11,7 @@ path = os.path.dirname(os.path.realpath(__file__)) + "/.." user = builtins.howdy_user # Check if enough arguments have been passed -if builtins.howdy_args.argument is None: +if not builtins.howdy_args.arguments: print("Please add the ID of the model you want to remove as an argument") print("For example:") print("\n\thowdy remove 0\n") @@ -39,9 +39,12 @@ except FileNotFoundError: # Tracks if a encoding with that id has been found found = False +# Get the ID from the cli arguments +id = builtins.howdy_args.arguments[0] + # Loop though all encodings and check if they match the argument for enc in encodings: - if str(enc["id"]) == builtins.howdy_args.argument: + if str(enc["id"]) == id: # Only ask the user if there's no -y flag if not builtins.howdy_args.y: # Double check with the user @@ -50,8 +53,8 @@ for enc in encodings: # Abort if the answer isn't yes if (ans.lower() != "y"): - print('\nInterpreting as a "NO"') - sys.exit() + print('\nInterpreting as a "NO", aborting') + sys.exit(1) # Add a padding empty line print() @@ -62,7 +65,7 @@ for enc in encodings: # Abort if no matching id was found if not found: - print("No model with ID " + builtins.howdy_args.argument + " exists for " + user) + print("No model with ID " + id + " exists for " + user) sys.exit(1) # Remove the entire file if this encoding is the only one @@ -73,13 +76,13 @@ else: # A place holder to contain the encodings that will remain new_encodings = [] - # Loop though all encodin and only add thos that don't need to be removed + # Loop though all encodings and only add those that don't need to be removed for enc in encodings: - if str(enc["id"]) != builtins.howdy_args.argument: + if str(enc["id"]) != id: new_encodings.append(enc) # Save this new set to disk with open(enc_file, "w") as datafile: json.dump(new_encodings, datafile) - print("Removed model " + builtins.howdy_args.argument) + print("Removed model " + id) diff --git a/src/cli/set.py b/src/cli/set.py new file mode 100644 index 0000000..6235133 --- /dev/null +++ b/src/cli/set.py @@ -0,0 +1,41 @@ +# Set a config value + +# Import required modules +import sys +import os +import builtins +import fileinput + +# Get the absolute filepath +config_path = os.path.dirname(os.path.abspath(__file__)) + "/../config.ini" + +# Check if enough arguments have been passed +if len(builtins.howdy_args.arguments) < 2: + print("Please add a setting you would like to change and the value to set it to") + print("For example:") + print("\n\thowdy set certainty 3\n") + sys.exit(1) + +# Get the name and value from the cli +set_name = builtins.howdy_args.arguments[0] +set_value = builtins.howdy_args.arguments[1] + +# Will be filled with the correctly config line to update +found_line = "" + +# Loop through all lines in the config file +for line in fileinput.input([config_path]): + # Save the line if it starts with the requested config option + if line.startswith(set_name + " "): + found_line = line + +# If we don't have the line it is not in the config file +if not found_line: + print('Could not find a "' + set_name + '" config option to set') + sys.exit(1) + +# Go through the file again and update the correct line +for line in fileinput.input([config_path], inplace=1): + print(line.replace(found_line, set_name + " = " + set_value + "\n"), end="") + +print("Config option updated")