# HG changeset patch # User Zach Welch zach@mandolincreekfarm.com
Update chirpc argument parsing (#2343)
The optparse module is deprecated, so this patch converts chirpc to use the argparse module. This allows chirpc and chirpw to share argument parsing code.
diff --git a/chirpc b/chirpc index 9e5c59c..b4525a7 100755 --- a/chirpc +++ b/chirpc @@ -18,8 +18,7 @@
import serial import sys -from optparse import OptionParser -import optparse +import argparse
from chirp import * from chirp import chirp_common, errors, idrp, directory, util @@ -34,126 +33,131 @@ def fail_missing_mmap():
RADIOS = directory.DRV_TO_RADIO
-def store_tone(option, opt, value, parser): - if value in chirp_common.TONES: +class ToneAction(argparse.Action): +# def __init__(self, **kwargs): +# super(ToneAction, self).__init__(**kwargs) + + def __call__(self, parser, namespace, value, option_string=None): + if value in chirp_common.TONES: + raise argparse.ArgumentError("Invalid tone valeu: %.1f" % value) + setattr(namespace, self.dest, value) + +class DTCSAction(argparse.Action): + def __call__(self, parser, namespace, value, option_string=None): + try: + value = int(value, 10) + except ValueError: + raise argparse.ArgumentError("Invalid DTCS value: %s" % value) + + if value not in chirp_common.DTCS_CODES: + raise argparse.ArgumentError("Invalid DTCS value: %03i" % value) + setattr(namespace, self.dest, value) + +class DTCSPolarityAction(argparse.Action): + def __call__(self, parser, namespace, value, option_string=None): + if value not in ["NN", "RN", "NR", "RR"]: + raise optparse.OptionValueError("Invaid DTCS polarity: %s" % value) setattr(parser.values, option.dest, value) - else: - raise optparse.OptionValueError("Invalid tone value: %.1f" % value)
-def store_dtcs(option, opt, value, parser): - try: - value = int(value, 10) - except ValueError: - raise optparse.OptionValueError("Invalid DTCS value: %s" % value)
- if value in chirp_common.DTCS_CODES: - setattr(parser.values, option.dest, value) - else: - raise optparse.OptionValueError("Invalid DTCS value: %03i" % value) - -def store_dtcspol(option, opt, value, parser): - if value not in ["NN", "RN", "NR", "RR"]: - raise optparse.OptionValueError("Invaid DTCS polarity: %s" % value) - - setattr(parser.values, option.dest, value) if __name__ == "__main__": - parser = OptionParser() - parser.add_option("-s", "--serial", dest="serial", + parser = argparse.ArgumentParser() + parser.add_argument("-s", "--serial", dest="serial", default="mmap", help="Serial port (default: mmap)")
- parser.add_option("-i", "--id", dest="id", + parser.add_argument("-i", "--id", dest="id", default=False, action="store_true", help="Request radio ID string") - parser.add_option("", "--raw", dest="raw", + parser.add_argument("--raw", dest="raw", default=False, action="store_true", help="Dump raw memory location")
- parser.add_option("", "--get-mem", dest="get_mem", + parser.add_argument("--get-mem", dest="get_mem", default=False, action="store_true", help="Get and print memory location") - parser.add_option("", "--set-mem-name", dest="set_mem_name", + parser.add_argument("--set-mem-name", dest="set_mem_name", default=None, help="Set memory name") - parser.add_option("", "--set-mem-freq", dest="set_mem_freq", - type="float", + parser.add_argument("--set-mem-freq", dest="set_mem_freq", + type=float, default=None, help="Set memory frequency")
- parser.add_option("", "--set-mem-tencon", dest="set_mem_tencon", + parser.add_argument("--set-mem-tencon", dest="set_mem_tencon", default=False, action="store_true", help="Set tone encode enabled flag") - parser.add_option("", "--set-mem-tencoff", dest="set_mem_tencoff", + parser.add_argument("--set-mem-tencoff", dest="set_mem_tencoff", default=False, action="store_true", help="Set tone decode disabled flag") - parser.add_option("", "--set-mem-tsqlon", dest="set_mem_tsqlon", + parser.add_argument("--set-mem-tsqlon", dest="set_mem_tsqlon", default=False, action="store_true", help="Set tone squelch enabled flag") - parser.add_option("", "--set-mem-tsqloff", dest="set_mem_tsqloff", + parser.add_argument("--set-mem-tsqloff", dest="set_mem_tsqloff", default=False, action="store_true", help="Set tone squelch disabled flag") - parser.add_option("", "--set-mem-dtcson", dest="set_mem_dtcson", + parser.add_argument("--set-mem-dtcson", dest="set_mem_dtcson", default=False, action="store_true", help="Set DTCS enabled flag") - parser.add_option("", "--set-mem-dtcsoff", dest="set_mem_dtcsoff", + parser.add_argument("--set-mem-dtcsoff", dest="set_mem_dtcsoff", default=False, action="store_true", help="Set DTCS disabled flag")
- parser.add_option("", "--set-mem-tenc", dest="set_mem_tenc", - type="float", - action="callback", callback=store_tone, nargs=1, + parser.add_argument("--set-mem-tenc", dest="set_mem_tenc", + type=float, action=ToneAction, nargs=1, help="Set memory encode tone") - parser.add_option("", "--set-mem-tsql", dest="set_mem_tsql", - type="float", - action="callback", callback=store_tone, nargs=1, + parser.add_argument("--set-mem-tsql", dest="set_mem_tsql", + type=float, action=ToneAction, nargs=1, help="Set memory squelch tone") - parser.add_option("", "--set-mem-dtcs", dest="set_mem_dtcs", - type="string", - action="callback", callback=store_dtcs, nargs=1, - help="Set memory DTCS code")
- parser.add_option("", "--set-mem-dtcspol", dest="set_mem_dtcspol", - type="string", - action="callback", callback=store_dtcspol, nargs=1, + parser.add_argument("--set-mem-dtcs", dest="set_mem_dtcs", + type=int, action=DTCSAction, nargs=1, + help="Set memory DTCS code") + parser.add_argument("--set-mem-dtcspol", dest="set_mem_dtcspol", + action=DTCSPolarityAction, nargs=1, help="Set memory DTCS polarity (NN, NR, RN, RR)")
- parser.add_option("", "--set-mem-dup", dest="set_mem_dup", + parser.add_argument("--set-mem-dup", dest="set_mem_dup", help="Set memory duplex (+,-, or blank)") - parser.add_option("", "--set-mem-offset", dest="set_mem_offset", - type="float", + parser.add_argument("--set-mem-offset", dest="set_mem_offset", + type=float, help="Set memory duplex offset (in MHz)")
- parser.add_option("", "--set-mem-mode", dest="set_mem_mode", + parser.add_argument("--set-mem-mode", dest="set_mem_mode", default=None, help="Set mode (%s)" % ",".join(chirp_common.MODES)) - parser.add_option("-r", "--radio", dest="radio", + parser.add_argument("-r", "--radio", dest="radio", default=None, help="Radio model (one of %s)" % ",".join(RADIOS.keys()))
- parser.add_option("", "--mmap", dest="mmap", + parser.add_argument("--mmap", dest="mmap", default=None, help="Radio memory map file location") - parser.add_option("", "--download-mmap", dest="download_mmap", + parser.add_argument("--download-mmap", dest="download_mmap", action="store_true", default=False, help="Download memory map from radio") - parser.add_option("", "--upload-mmap", dest="upload_mmap", + parser.add_argument("--upload-mmap", dest="upload_mmap", action="store_true", default=False, help="Upload memory map to radio") + parser.add_argument("args", metavar="arg", nargs='*', + help="Some commands require additional arguments") if len(sys.argv) <= 1: parser.print_help() sys.exit(0) - (options, args) = parser.parse_args() + + options = parser.parse_args() + args = options.args
if options.id: from chirp import icf