Change in pysim[master]: pySim-shell: Migrate from optparse to argparse for the main()

laforge gerrit-no-reply at lists.osmocom.org
Mon Apr 12 18:49:13 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/23718 )

Change subject: pySim-shell: Migrate from optparse to argparse for the main()
......................................................................

pySim-shell: Migrate from optparse to argparse for the main()

We're using argparse internally for all shell commands, and can
use that to auto-generate command reference in the manual.

Let's switch to argparse for the main program, too - and generate
the related reference in the manual.

Change-Id: I77c946dbeb9f746fe3d8051173e59462dc2fb5e2
---
M docs/shell.rst
M pySim-shell.py
2 files changed, 37 insertions(+), 54 deletions(-)

Approvals:
  Jenkins Builder: Verified
  fixeria: Looks good to me, approved



diff --git a/docs/shell.rst b/docs/shell.rst
index 30938fe..4cdf9e0 100644
--- a/docs/shell.rst
+++ b/docs/shell.rst
@@ -23,6 +23,20 @@
 * tab completion of commands and SELECT-able files/directories
 * interactive help for all commands
 
+Running pySim-shell
+-------------------
+
+pySim-shell has a variety of command line arguments to control
+
+* which transport to use (how to use a reader to talk to the SIM card)
+* whether to automatically verify an ADM pin (and in which format)
+* whether to execute a start-up script
+
+.. argparse::
+   :module: pySim-shell
+   :func: option_parser
+
+
 
 cmd2 basics
 -----------
diff --git a/pySim-shell.py b/pySim-shell.py
index 5298cb4..6bc9a95 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -28,7 +28,6 @@
 
 import os
 import sys
-from optparse import OptionParser
 from pathlib import Path
 
 from pySim.ts_51_011 import EF, DF, EF_SST_map, EF_AD_mode_map
@@ -430,64 +429,34 @@
 		(data, sw) = self._cmd.card._scc.manage_channel(mode='close', lchan_nr=opts.chan_nr)
 
 
-def parse_options():
-
-	parser = OptionParser(usage="usage: %prog [options]")
-
-	parser.add_option("-d", "--device", dest="device", metavar="DEV",
-			help="Serial Device for SIM access [default: %default]",
-			default="/dev/ttyUSB0",
-		)
-	parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
-			help="Baudrate used for SIM access [default: %default]",
-			default=9600,
-		)
-	parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
-			help="Which PC/SC reader number for SIM access",
-			default=None,
-		)
-	parser.add_option("--modem-device", dest="modem_dev", metavar="DEV",
-			help="Serial port of modem for Generic SIM Access (3GPP TS 27.007)",
-			default=None,
-		)
-	parser.add_option("--modem-baud", dest="modem_baud", type="int", metavar="BAUD",
-			help="Baudrate used for modem's port [default: %default]",
-			default=115200,
-		)
-	parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH",
-			help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)",
-			default=None,
-		)
-	parser.add_option("--script", dest="script", metavar="PATH",
-			help="script with shell commands to be executed automatically",
-			default=None,
-		)
-
-	parser.add_option("--csv", dest="csv", metavar="FILE",
-			help="Read card data from CSV file",
-			default=None,
-		)
-
-	parser.add_option("-a", "--pin-adm", dest="pin_adm",
-			help="ADM PIN used for provisioning (overwrites default)",
-		)
-	parser.add_option("-A", "--pin-adm-hex", dest="pin_adm_hex",
-			help="ADM PIN used for provisioning, as hex string (16 characters long",
-		)
-
-	(options, args) = parser.parse_args()
-
-	if args:
-		parser.error("Extraneous arguments")
-
-	return options
-
+option_parser = argparse.ArgumentParser(prog='pySim-shell', description='interactive SIM card shell',
+                                        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
+option_parser.add_argument('-d', '--device', metavar='DEV', default='/dev/ttyUSB0',
+                           help='Serial Device for SIM access')
+option_parser.add_argument('-b', '--baud', dest='baudrate', type=int, metavar='BAUD', default=9600,
+                           help='Baud rate used for SIM access')
+option_parser.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None,
+                           help='PC/SC reader number to use for SIM access')
+option_parser.add_argument('--modem-device', dest='modem_dev', metavar='DEV', default=None,
+                           help='Serial port of modem for Generic SIM Access (3GPP TS 27.007)')
+option_parser.add_argument('--modem-baud', type=int, metavar='BAUD', default=115200,
+                           help='Baud rate used for modem port')
+option_parser.add_argument('--osmocon', dest='osmocon_sock', metavar='PATH', default=None,
+                           help='Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)')
+option_parser.add_argument('--script', metavar='PATH', default=None,
+                           help='script with pySim-shell commands to be executed automatically at start-up')
+option_parser.add_argument('--csv', metavar='FILE', default=None,
+                           help='Read card data from CSV file')
+option_parser.add_argument('-a', '--pin-adm', metavar='PIN_ADM1', dest='pin_adm', default=None,
+                           help='ADM PIN used for provisioning (overwrites default)')
+option_parser.add_argument('-A', '--pin-adm-hex', metavar='PIN_ADM1_HEX', dest='pin_adm_hex', default=None,
+                           help='ADM PIN used for provisioning, as hex string (16 characters long)')
 
 
 if __name__ == '__main__':
 
 	# Parse options
-	opts = parse_options()
+	opts = option_parser.parse_args()
 
 	# Init card reader driver
 	sl = init_reader(opts)

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23718
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I77c946dbeb9f746fe3d8051173e59462dc2fb5e2
Gerrit-Change-Number: 23718
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: tnt <tnt at 246tNt.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210412/6170a188/attachment.htm>


More information about the gerrit-log mailing list