From fd6d74da8a29505c26cd2cd2f96c352e0850e9ed Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Fri, 11 Feb 2022 15:56:50 -0800 Subject: [PATCH 1/9] Parsing for mod printer --- staff/lab/mod-printer | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 staff/lab/mod-printer diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer new file mode 100644 index 0000000..4de4a1d --- /dev/null +++ b/staff/lab/mod-printer @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +"""Remove or add printers to/from CUPS classes.""" +import argparse +import subprocess +import sys + +def main(args): + # Determine which CUPS classes are available + cups_classes = subprocess.check_output(["/usr/bin/lpstat", "-p"]).split("\n") + for i in range(len(cups_classes)): + clazz = cups_classes[i] + # Output is of the form: printer is idle. + clazz = clazz[len("printer "):] + clazz = clazz[:clazz.index(" is ")] + cups_classes[i] = clazz + + for clazz in cups_classes: + subprocess.check_output(["/usr/sbin/lpadmin", "-p", args.printer, "-r", clazz]) + +def parse_args(): + args = argparse.ArgumentParser(description=__doc__) + args.add_argument("action", + choices=["add", "remove"], + help="Action to perform" + ) + args.add_argument("printer", + help="Name of printer to modify" + ) + + return args.parse_args() + +if __name__ == "__main__": + args = parse_args() + sys.exit(main(args)) \ No newline at end of file From beacde610b372542107aea884967c73a6dcabd83 Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Fri, 11 Feb 2022 18:31:41 -0800 Subject: [PATCH 2/9] Implemented mod-printer functionality --- staff/lab/mod-printer | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer index 4de4a1d..37df6b5 100644 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -4,7 +4,9 @@ import argparse import subprocess import sys -def main(args): + +def modify_printer(args): + """Perform internal call to CUPS CLI to modify class(es).""" # Determine which CUPS classes are available cups_classes = subprocess.check_output(["/usr/bin/lpstat", "-p"]).split("\n") for i in range(len(cups_classes)): @@ -13,22 +15,47 @@ def main(args): clazz = clazz[len("printer "):] clazz = clazz[:clazz.index(" is ")] cups_classes[i] = clazz + + if args.verbose: + print("Found CUPS classes:", cups_classes) + + args_clazz = getattr(args, "class") + if args_clazz and args_clazz in cups_classes: + cups_classes = [args_clazz] + if args.verbose: + print("Overriding CUPS classes with parameter:", args_clazz) + # Call lpadmin command to perform addition/removal + printer = args.printer.lower() + if args.verbose: + print("Printer to perform action on:", printer) + action_flag = "-r" if args.action == "remove" else "-c" + if args.verbose: + print("Action to be performed (and flag):", args.action, "(" + action_flag + ")") for clazz in cups_classes: - subprocess.check_output(["/usr/sbin/lpadmin", "-p", args.printer, "-r", clazz]) + action_retval = subprocess.check_output(["/usr/sbin/lpadmin", "-p", printer + "-" + clazz, action_flag, clazz]) + if action_retval: + print("ERROR: lpadmin returned a non-zero value", action_retval) + return 1 + print("Printer", printer, "was successfully", args.action + "(ed)", "from the CUPS class(es)", cups_classes) -def parse_args(): +def main(): args = argparse.ArgumentParser(description=__doc__) args.add_argument("action", choices=["add", "remove"], help="Action to perform" ) - args.add_argument("printer", - help="Name of printer to modify" + args.add_argument("printer", help="Name of printer to modify") + + args.add_argument("-c", "--class", help="Remove from only a specific class") + args.add_argument("-v", "--verbose", "--debug", + action="store_true", + help="Output more verbose logging to stdout" ) - return args.parse_args() + args = args.parse_args() + return modify_printer(args) + if __name__ == "__main__": - args = parse_args() - sys.exit(main(args)) \ No newline at end of file + sys.exit(main()) \ No newline at end of file From 02c21e21ffd5897cacd553f0958b725a58d9e471 Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Wed, 16 Feb 2022 13:46:19 -0800 Subject: [PATCH 3/9] Test and bugfix mod-printer --- staff/lab/mod-printer | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) mode change 100644 => 100755 staff/lab/mod-printer diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer old mode 100644 new mode 100755 index 37df6b5..fb8d8c6 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -8,7 +8,9 @@ import sys def modify_printer(args): """Perform internal call to CUPS CLI to modify class(es).""" # Determine which CUPS classes are available - cups_classes = subprocess.check_output(["/usr/bin/lpstat", "-p"]).split("\n") + cups_classes = subprocess.check_output(["lpstat", "-p"]).decode("utf-8").split("\n") + # Remove empty trailing newline + cups_classes.remove("") for i in range(len(cups_classes)): clazz = cups_classes[i] # Output is of the form: printer is idle. @@ -17,27 +19,33 @@ def modify_printer(args): cups_classes[i] = clazz if args.verbose: - print("Found CUPS classes:", cups_classes) + print("Found CUPS classes: " + str(cups_classes)) args_clazz = getattr(args, "class") if args_clazz and args_clazz in cups_classes: cups_classes = [args_clazz] if args.verbose: - print("Overriding CUPS classes with parameter:", args_clazz) + print("Overriding CUPS classes with parameter: " + str(args_clazz)) - # Call lpadmin command to perform addition/removal + # Perform pre-call logging and input processing printer = args.printer.lower() if args.verbose: - print("Printer to perform action on:", printer) + print("Printer to perform action on: " + printer) action_flag = "-r" if args.action == "remove" else "-c" if args.verbose: - print("Action to be performed (and flag):", args.action, "(" + action_flag + ")") + print("Action to be performed (and flag): " + args.action, "(" + action_flag + ")") + + # Call lpadmin command to perform addition/removal for clazz in cups_classes: - action_retval = subprocess.check_output(["/usr/sbin/lpadmin", "-p", printer + "-" + clazz, action_flag, clazz]) - if action_retval: - print("ERROR: lpadmin returned a non-zero value", action_retval) + action_cmd = subprocess.Popen(["lpadmin", "-p", printer + "-" + clazz, action_flag, clazz]) + out, err = action_cmd.communicate() + if action_cmd.returncode: + print("ERROR: lpadmin returned a non-zero value " + str(action_cmd.returncode)) + return 1 + if err: + print("ERROR: an error occurred while running lpadmin - " + str(err)) return 1 - print("Printer", printer, "was successfully", args.action + "(ed)", "from the CUPS class(es)", cups_classes) + print("Printer " + printer + " was successfully " + args.action + "(ed) to/from the CUPS class(es): " + str(cups_classes)) def main(): args = argparse.ArgumentParser(description=__doc__) From 5412b026134e6c7be5a07709dd53abfcb8493021 Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Sat, 19 Feb 2022 04:04:38 -0800 Subject: [PATCH 4/9] Update mod-printer with more modern Python functions --- staff/lab/mod-printer | 63 ++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer index fb8d8c6..933e7c4 100755 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """Remove or add printers to/from CUPS classes.""" import argparse +import re import subprocess import sys @@ -8,44 +9,50 @@ import sys def modify_printer(args): """Perform internal call to CUPS CLI to modify class(es).""" # Determine which CUPS classes are available - cups_classes = subprocess.check_output(["lpstat", "-p"]).decode("utf-8").split("\n") - # Remove empty trailing newline - cups_classes.remove("") - for i in range(len(cups_classes)): - clazz = cups_classes[i] - # Output is of the form: printer is idle. - clazz = clazz[len("printer "):] - clazz = clazz[:clazz.index(" is ")] - cups_classes[i] = clazz + try: + lpstat_out = subprocess.run(["lpstat", "-p"], check=True, \ + stdout=subprocess.PIPE, stdin=subprocess.PIPE).strip().split("\n") + except subprocess.CalledProcessError as e: + print("ERROR: lpstat process exited with exit code %i" % e.returncode) + return e.returncode + + # Parse lpstat output for printer class names + # Output is of the form: printer is idle. + cups_classes = [re.search(r"printer (\S*) is", classname).group(1) for classname in lpstat_out] if args.verbose: - print("Found CUPS classes: " + str(cups_classes)) + print("Found CUPS classes: %s" % cups_classes) - args_clazz = getattr(args, "class") - if args_clazz and args_clazz in cups_classes: - cups_classes = [args_clazz] - if args.verbose: - print("Overriding CUPS classes with parameter: " + str(args_clazz)) + # Override with specified classname if provided and valid + if args.classname: + if args.classname in cups_classes: + cups_classes = [args.classname] + if args.verbose: + print("Overriding CUPS classes with parameter: %s" % args.classname) + else: + print("ERROR: Specified class %s could not be found in available classes %s" % (args.classname, cups_classes)) + return 1 # Perform pre-call logging and input processing printer = args.printer.lower() if args.verbose: - print("Printer to perform action on: " + printer) + print("Printer to perform action on: %s" % printer) action_flag = "-r" if args.action == "remove" else "-c" if args.verbose: - print("Action to be performed (and flag): " + args.action, "(" + action_flag + ")") + print("Action to be performed (and flag): %s (%s)" % (args.action, action_flag)) # Call lpadmin command to perform addition/removal - for clazz in cups_classes: - action_cmd = subprocess.Popen(["lpadmin", "-p", printer + "-" + clazz, action_flag, clazz]) - out, err = action_cmd.communicate() - if action_cmd.returncode: - print("ERROR: lpadmin returned a non-zero value " + str(action_cmd.returncode)) - return 1 - if err: - print("ERROR: an error occurred while running lpadmin - " + str(err)) - return 1 - print("Printer " + printer + " was successfully " + args.action + "(ed) to/from the CUPS class(es): " + str(cups_classes)) + for classname in cups_classes: + action_cmd = ["lpadmin", "-p", printer + "-" + classname, action_flag, classname] + try: + subprocess.run(action_cmd, check=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) + except subprocess.CalledProcessError as e: + print("ERROR: lpstat process exited with exit code %i" % e.returncode) + return e.returncode + else: + action_out = "added to" if args.action == "add" else "removed from" + print("Printer %s was successfully %s %s" % (printer, action_out, classname)) + def main(): args = argparse.ArgumentParser(description=__doc__) @@ -55,7 +62,7 @@ def main(): ) args.add_argument("printer", help="Name of printer to modify") - args.add_argument("-c", "--class", help="Remove from only a specific class") + args.add_argument("-c", "--classname", help="Modify only a specific class") args.add_argument("-v", "--verbose", "--debug", action="store_true", help="Output more verbose logging to stdout" From f8804869ce3fb7e45e3f763062ce5b360e85f83d Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Sat, 19 Feb 2022 12:14:35 -0800 Subject: [PATCH 5/9] Change mod-printer to subparsers --- staff/lab/mod-printer | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer index 933e7c4..c17e901 100755 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -54,22 +54,39 @@ def modify_printer(args): print("Printer %s was successfully %s %s" % (printer, action_out, classname)) +def list_printers(args): + #TODO figure out what CUPS CLI calls this will use and parse data + pass + + def main(): - args = argparse.ArgumentParser(description=__doc__) - args.add_argument("action", - choices=["add", "remove"], - help="Action to perform" - ) - args.add_argument("printer", help="Name of printer to modify") - - args.add_argument("-c", "--classname", help="Modify only a specific class") - args.add_argument("-v", "--verbose", "--debug", + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("-v", "--verbose", "--debug", action="store_true", - help="Output more verbose logging to stdout" + help="output more verbose logging to stdout" ) + parser.add_argument("-c", "--classname", help="modify/list only a specific class") + + subparsers = parser.add_subparsers(dest="action", help="action to perform", required=True) + + add_parser = subparsers.add_parser("add", help="add a printer") + add_parser.add_argument("printer", help="name of printer to add") + + remove_parser = subparsers.add_parser("remove", help="remove a printer") + remove_parser.add_argument("printer", help="name of printer to remove") + + list_parser = subparsers.add_parser("list", help="list printer statuses") + list_parser.add_argument("-p", "--printer", help="list only this named printer") + list_parser.add_argument("-j", "--jobs", help="include print jobs") - args = args.parse_args() - return modify_printer(args) + args = parser.parse_args() + if args.action == "add" or args.action == "remove": + return modify_printer(args) + elif args.action == "list": + return list_printers(args) + else: + print("ERROR: Invalid action passed %s" % args.action) + return 1 if __name__ == "__main__": From 3b8219b2b1f5c11c615e54285f180a219965b62f Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Thu, 24 Feb 2022 01:18:40 -0800 Subject: [PATCH 6/9] Add list_printers mod-printer base function --- staff/lab/mod-printer | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer index c17e901..493de77 100755 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -55,8 +55,35 @@ def modify_printer(args): def list_printers(args): - #TODO figure out what CUPS CLI calls this will use and parse data - pass + """Perform internal call to CUPS CLI to list printer status(es) (and potentially jobs).""" + # Initialize base lpstat command + lpstat_cmd = ["lpstat", "-c"] + + # Add on to lpstat command with arguments + if args.classname: + if args.verbose: + print("Limiting results to only class %s" % args.classname) + lpstat_cmd.append(args.classname) + if args.jobs: + if args.verbose: + print("Including jobs in stdout") + lpstat_cmd.append("-o") + + if args.verbose: + print("Calling %s to list printer status" % lpstat_cmd) + + try: + # Call lpstat and send output directly to stdout + # Command already prints members of the class and can + # be configured to include jobs as well. No way to + # check for printers not in classes. + lpstat_out = subprocess.run(lpstat_cmd, check=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) + except subprocess.CalledProcessError as e: + print("ERROR: lpstat process exited with exit code %i" % e.returncode) + return e.returncode + + #TODO more advanced processing on output + #TODO include printer option in ^^^ def main(): @@ -77,7 +104,8 @@ def main(): list_parser = subparsers.add_parser("list", help="list printer statuses") list_parser.add_argument("-p", "--printer", help="list only this named printer") - list_parser.add_argument("-j", "--jobs", help="include print jobs") + list_parser.add_argument("-j", "--jobs", + action="store_true", help="include print jobs") args = parser.parse_args() if args.action == "add" or args.action == "remove": From 5001509822b508334e93ec120f12e3b140438108 Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Fri, 25 Feb 2022 11:46:19 -0800 Subject: [PATCH 7/9] Add list subcommand to mod-printer --- staff/lab/mod-printer | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer index 493de77..64189b7 100755 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -73,8 +73,7 @@ def list_printers(args): print("Calling %s to list printer status" % lpstat_cmd) try: - # Call lpstat and send output directly to stdout - # Command already prints members of the class and can + # Call lpstat -prints members of the class and can # be configured to include jobs as well. No way to # check for printers not in classes. lpstat_out = subprocess.run(lpstat_cmd, check=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) @@ -82,8 +81,19 @@ def list_printers(args): print("ERROR: lpstat process exited with exit code %i" % e.returncode) return e.returncode - #TODO more advanced processing on output - #TODO include printer option in ^^^ + # Parse output to fit args + status_msgs = lpstat_out.stdout.decode("utf-8") + if args.printer: + status_lines = status_msgs.split("\n") + for line in status_lines[:]: + if args.printer not in line and "members of class" not in line: + status_lines.remove(line) + if args.verbose: + print("Removed \"%s\" from output to fit printer parameter of \"%s\"" % (line, args.printer)) + print("\n".join(status_lines)) + else: + print(status_msgs, end="") + def main(): From aea3261e9bed597049b6f00ff32bea34c379c9b9 Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Fri, 25 Feb 2022 11:52:29 -0800 Subject: [PATCH 8/9] Minor mod-printer doc fix --- staff/lab/mod-printer | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer index 64189b7..2bcdb5e 100755 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -73,7 +73,7 @@ def list_printers(args): print("Calling %s to list printer status" % lpstat_cmd) try: - # Call lpstat -prints members of the class and can + # Call lpstat - prints members of the class and can # be configured to include jobs as well. No way to # check for printers not in classes. lpstat_out = subprocess.run(lpstat_cmd, check=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) @@ -81,7 +81,7 @@ def list_printers(args): print("ERROR: lpstat process exited with exit code %i" % e.returncode) return e.returncode - # Parse output to fit args + # Parse output to fit printer arg, or send directly to stdout status_msgs = lpstat_out.stdout.decode("utf-8") if args.printer: status_lines = status_msgs.split("\n") From 46afd3117fd2bdbbcd92cf5a52f8f294d33f3fdc Mon Sep 17 00:00:00 2001 From: Boomaa23 Date: Fri, 25 Feb 2022 16:53:16 -0800 Subject: [PATCH 9/9] Fix mod-printer formatting, general verbosity --- staff/lab/mod-printer | 134 ++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 77 deletions(-) diff --git a/staff/lab/mod-printer b/staff/lab/mod-printer index 2bcdb5e..5af3472 100755 --- a/staff/lab/mod-printer +++ b/staff/lab/mod-printer @@ -9,123 +9,103 @@ import sys def modify_printer(args): """Perform internal call to CUPS CLI to modify class(es).""" # Determine which CUPS classes are available - try: - lpstat_out = subprocess.run(["lpstat", "-p"], check=True, \ - stdout=subprocess.PIPE, stdin=subprocess.PIPE).strip().split("\n") - except subprocess.CalledProcessError as e: - print("ERROR: lpstat process exited with exit code %i" % e.returncode) - return e.returncode - + lpstat_proc = subprocess.run(['lpstat', '-p'], check=True, + stdout=subprocess.PIPE, stdin=subprocess.PIPE) + lpstat_out = lpstat_proc.stdout.decode('utf-8').strip().split('\n') + # Parse lpstat output for printer class names # Output is of the form: printer is idle. - cups_classes = [re.search(r"printer (\S*) is", classname).group(1) for classname in lpstat_out] + cups_classes = [re.search(r'printer (\S*) is', classname).group(1) for classname in lpstat_out] if args.verbose: - print("Found CUPS classes: %s" % cups_classes) - + print('Found CUPS classes: %s' % cups_classes) + # Override with specified classname if provided and valid if args.classname: if args.classname in cups_classes: cups_classes = [args.classname] if args.verbose: - print("Overriding CUPS classes with parameter: %s" % args.classname) + print('Overriding CUPS classes with parameter: %s' % args.classname) else: - print("ERROR: Specified class %s could not be found in available classes %s" % (args.classname, cups_classes)) + print('ERROR: Specified class %s could not be found in available classes %s' + % (args.classname, cups_classes)) return 1 - + # Perform pre-call logging and input processing printer = args.printer.lower() if args.verbose: - print("Printer to perform action on: %s" % printer) - action_flag = "-r" if args.action == "remove" else "-c" + print('Printer to perform action on: %s' % printer) + action_flag = '-r' if args.action == 'remove' else '-c' if args.verbose: - print("Action to be performed (and flag): %s (%s)" % (args.action, action_flag)) - + print('Action to be performed (and flag): %s (%s)' % (args.action, action_flag)) + # Call lpadmin command to perform addition/removal for classname in cups_classes: - action_cmd = ["lpadmin", "-p", printer + "-" + classname, action_flag, classname] - try: - subprocess.run(action_cmd, check=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) - except subprocess.CalledProcessError as e: - print("ERROR: lpstat process exited with exit code %i" % e.returncode) - return e.returncode - else: - action_out = "added to" if args.action == "add" else "removed from" - print("Printer %s was successfully %s %s" % (printer, action_out, classname)) - + action_cmd = ['lpadmin', '-p', printer + '-' + classname, action_flag, classname] + subprocess.run(action_cmd, check=True) + action_out = 'added to' if args.action == 'add' else 'removed from' + print('Printer %s was successfully %s %s' % (printer, action_out, classname)) + def list_printers(args): """Perform internal call to CUPS CLI to list printer status(es) (and potentially jobs).""" # Initialize base lpstat command - lpstat_cmd = ["lpstat", "-c"] + lpstat_cmd = ['lpstat', '-c'] # Add on to lpstat command with arguments if args.classname: - if args.verbose: - print("Limiting results to only class %s" % args.classname) lpstat_cmd.append(args.classname) if args.jobs: - if args.verbose: - print("Including jobs in stdout") - lpstat_cmd.append("-o") + lpstat_cmd.append('-o') if args.verbose: - print("Calling %s to list printer status" % lpstat_cmd) + print('Calling %s to list printer status' % lpstat_cmd) - try: - # Call lpstat - prints members of the class and can - # be configured to include jobs as well. No way to - # check for printers not in classes. - lpstat_out = subprocess.run(lpstat_cmd, check=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) - except subprocess.CalledProcessError as e: - print("ERROR: lpstat process exited with exit code %i" % e.returncode) - return e.returncode + # Call lpstat - prints members of the class and can + # be configured to include jobs as well. No way to + # check for printers not in classes. + lpstat_proc = subprocess.run(lpstat_cmd, check=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) # Parse output to fit printer arg, or send directly to stdout - status_msgs = lpstat_out.stdout.decode("utf-8") + status_msgs = lpstat_proc.stdout.decode('utf-8') if args.printer: - status_lines = status_msgs.split("\n") - for line in status_lines[:]: - if args.printer not in line and "members of class" not in line: - status_lines.remove(line) - if args.verbose: - print("Removed \"%s\" from output to fit printer parameter of \"%s\"" % (line, args.printer)) - print("\n".join(status_lines)) + for line in status_msgs.split('\n'): + if args.printer in line or 'members of class' in line: + print(line) else: - print(status_msgs, end="") - + print(status_msgs, end='') def main(): parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("-v", "--verbose", "--debug", - action="store_true", - help="output more verbose logging to stdout" - ) - parser.add_argument("-c", "--classname", help="modify/list only a specific class") - - subparsers = parser.add_subparsers(dest="action", help="action to perform", required=True) - - add_parser = subparsers.add_parser("add", help="add a printer") - add_parser.add_argument("printer", help="name of printer to add") - - remove_parser = subparsers.add_parser("remove", help="remove a printer") - remove_parser.add_argument("printer", help="name of printer to remove") - - list_parser = subparsers.add_parser("list", help="list printer statuses") - list_parser.add_argument("-p", "--printer", help="list only this named printer") - list_parser.add_argument("-j", "--jobs", - action="store_true", help="include print jobs") - + parser.add_argument('-v', '--verbose', '--debug', + action='store_true', + help='output more verbose logging to stdout' + ) + parser.add_argument('-c', '--classname', help='modify/list only a specific class') + + subparsers = parser.add_subparsers(dest='action', help='action to perform', required=True) + + add_parser = subparsers.add_parser('add', help='add a printer') + add_parser.add_argument('printer', help='name of printer to add') + + remove_parser = subparsers.add_parser('remove', help='remove a printer') + remove_parser.add_argument('printer', help='name of printer to remove') + + list_parser = subparsers.add_parser('list', help='list printer statuses') + list_parser.add_argument('-p', '--printer', help='list only this named printer') + list_parser.add_argument('-j', '--jobs', + action='store_true', help='include print jobs') + args = parser.parse_args() - if args.action == "add" or args.action == "remove": + if args.action == 'add' or args.action == 'remove': return modify_printer(args) - elif args.action == "list": + elif args.action == 'list': return list_printers(args) else: - print("ERROR: Invalid action passed %s" % args.action) + print('ERROR: Invalid action passed %s' % args.action) return 1 - -if __name__ == "__main__": - sys.exit(main()) \ No newline at end of file + +if __name__ == '__main__': + sys.exit(main())