Browse code

Replace third party `docopt` with `argparse`

Robert Cranston authored on 09/05/2025 15:03:38
Showing 3 changed files

... ...
@@ -10,23 +10,18 @@ Parse [Raspberry Pi revision codes][].
10 10
 `raspi-revcode --help`:
11 11
 
12 12
 ```
13
-usage:
14
-  raspi-revcode [<code>]
15
-  raspi-revcode -h|--help
16
-  raspi-revcode --version
13
+usage: raspi_revcode.py [-h] [--version] [<code>]
17 14
 
18 15
 Parse Raspberry Pi revision codes.
19 16
 
20 17
 positional arguments:
21
-  <code>
22
-    A hexadecimal revision code to parse. If not given, '/proc/cpuinfo' will be
23
-    used to get the revision code of the current device.
18
+  <code>      A hexadecimal revision code to parse. If not given,
19
+              '/proc/cpuinfo' will be used to get the revision code of the
20
+              current device.
24 21
 
25 22
 optional arguments:
26
-  -h, --help
27
-    Show this help message and exit.
28
-  --version
29
-    Show program's version number and exit.
23
+  -h, --help  show this help message and exit
24
+  --version   show program's version number and exit
30 25
 ```
31 26
 
32 27
 ## Examples
... ...
@@ -72,6 +67,14 @@ Overvoltage  : Overvoltage disallowed
72 67
 
73 68
 ## Install
74 69
 
70
+`raspi-revcode` depends only on the Python standard library which means that,
71
+if [Python is installed][Python download], it can run be as a standalone script
72
+without installation:
73
+
74
+```sh
75
+./raspi_revcode.py
76
+```
77
+
75 78
 ### Prerequisites
76 79
 
77 80
 Make sure [Python is installed][Python download], [`pip`][] is available and
... ...
@@ -1,32 +1,10 @@
1 1
 #!/usr/bin/env python3
2 2
 
3
-## Help
4
-
5
-"""
6
-usage:
7
-  raspi-revcode [<code>]
8
-  raspi-revcode -h|--help
9
-  raspi-revcode --version
10
-
11
-Parse Raspberry Pi revision codes.
12
-
13
-positional arguments:
14
-  <code>
15
-    A hexadecimal revision code to parse. If not given, '/proc/cpuinfo' will be
16
-    used to get the revision code of the current device.
17
-
18
-optional arguments:
19
-  -h, --help
20
-    Show this help message and exit.
21
-  --version
22
-    Show program's version number and exit.
23
-"""
24
-
25 3
 ## Imports
26 4
 
27 5
 import sys
28 6
 import re
29
-import docopt
7
+import argparse
30 8
 
31 9
 ## Constants
32 10
 
... ...
@@ -176,7 +154,15 @@ def parse(code):
176 154
 
177 155
 def main():
178 156
     # Parse arguments.
179
-    args = docopt.docopt(__doc__, version=VERSION)
157
+    parser = argparse.ArgumentParser(description="""
158
+        Parse Raspberry Pi revision codes.
159
+    """);
160
+    parser.add_argument('--version', action='version', version=VERSION)
161
+    parser.add_argument('<code>', nargs='?', help="""
162
+        A hexadecimal revision code to parse. If not given, '/proc/cpuinfo'
163
+        will be used to get the revision code of the current device.
164
+    """)
165
+    args = vars(parser.parse_args())
180 166
     # Get code string from argument.
181 167
     code = args['<code>']
182 168
     # Get code string from system.
... ...
@@ -19,7 +19,6 @@ setup(
19 19
     ],
20 20
     python_requires='>=3, <4',
21 21
     install_requires=[
22
-        'docopt',
23 22
     ],
24 23
     py_modules=['raspi_revcode'],
25 24
     entry_points={