1
1
# -*- coding: utf-8 -*-
2
2
3
3
import sys
4
- import argparse
4
+
5
+ from argparse import ArgumentParser
6
+ from argparse import RawTextHelpFormatter
7
+ from argparse import SUPPRESS
8
+ from argparse import _HelpAction
5
9
6
10
from os import sep
7
11
from os .path import isdir
@@ -26,81 +30,70 @@ def parse_args(args):
26
30
summary = dict (
27
31
usage = header ,
28
32
description = meta .get ('description' ),
29
- epilog = 'More details on ' + meta .get ('url' )
33
+ epilog = 'More details on ' + meta .get ('url' ),
34
+ formatter_class = RawTextHelpFormatter ,
35
+ add_help = False
30
36
)
31
-
32
- parser = argparse .ArgumentParser (** summary )
37
+ p = ArgumentParser (** summary )
33
38
34
39
# Remove the default arguments group:
35
- parser ._action_groups .pop ()
36
-
37
- # Required arguments:
38
- required = parser .add_argument_group ('Required arguments' )
39
- required .add_argument ('--input' , '-i' ,
40
- required = True ,
41
- help = ('Path to an exported estimator in pickle '
42
- '(.pkl) format.' ))
40
+ p ._action_groups .pop ()
41
+
42
+ # File arguments:
43
+ files = p .add_argument_group ('File arguments' )
44
+ help = 'Path to an exported estimator in pickle (.pkl) format.'
45
+ files .add_argument ('input' , help = help )
46
+ help = 'Path to the output directory where ' \
47
+ 'the transpiled estimator will be stored.'
48
+ files .add_argument ('--to' , required = False , help = help )
49
+
50
+ # Template arguments:
51
+ templates = p .add_argument_group ('Template arguments' )
52
+ templates .add_argument ('--class_name' , default = None , required = False ,
53
+ help = 'Define a custom class name.' )
54
+ templates .add_argument ('--method_name' , default = 'predict' , required = False ,
55
+ help = 'Define a custom method name.' )
43
56
44
57
# Optional arguments:
45
- optional = parser .add_argument_group ('Optional arguments' )
46
- optional .add_argument ('--output' , '-o' ,
47
- required = False ,
48
- help = ('Path to the destination directory where the '
49
- 'transpiled estimator will be stored.' ))
50
- optional .add_argument ('--class_name' ,
51
- default = None ,
52
- required = False ,
53
- help = 'Define the class name in the final output.' )
54
- optional .add_argument ('--method_name' ,
55
- default = 'predict' ,
56
- required = False ,
57
- help = 'Define the method name in the final output.' )
58
- optional .add_argument ('--export' , '-e' ,
59
- required = False ,
60
- default = False ,
61
- action = 'store_true' ,
62
- help = 'Whether to export the model data or not.' )
63
- optional .add_argument ('--checksum' , '-s' ,
64
- required = False ,
65
- default = False ,
66
- action = 'store_true' ,
67
- help = 'Whether to append the checksum to the '
68
- 'filename or not.' )
69
- optional .add_argument ('--data' , '-d' ,
70
- required = False ,
71
- default = False ,
72
- action = 'store_true' ,
73
- help = 'Whether to export just the model data or all.' )
74
- optional .add_argument ('--pipe' , '-p' ,
75
- required = False ,
76
- default = False ,
77
- action = 'store_true' ,
78
- help = 'Print the transpiled estimator to the console.' )
79
-
80
- # Languages:
81
- langs = parser .add_argument_group ('Programming languages' )
58
+ optional = p .add_argument_group ('Optional arguments' )
59
+ optional .add_argument ('--export' , '-e' , required = False , default = False ,
60
+ action = 'store_true' , help = 'Whether to export '
61
+ 'the model data or not.' )
62
+ optional .add_argument ('--checksum' , '-s' , required = False , default = False ,
63
+ action = 'store_true' , help = 'Whether to append the '
64
+ 'checksum to the filename '
65
+ 'or not.' )
66
+ optional .add_argument ('--data' , '-d' , required = False , default = False ,
67
+ action = 'store_true' , help = 'Whether to export just '
68
+ 'the model data or all.' )
69
+ optional .add_argument ('--pipe' , '-p' , required = False , default = False ,
70
+ action = 'store_true' , help = 'Print the transpiled '
71
+ 'estimator to the console.' )
72
+
73
+ # Programming languages:
74
+ langs = p .add_argument_group ('Programming languages' )
82
75
languages = {key : clazz .LABEL for key , clazz in list (LANGUAGES .items ())}
83
- langs .add_argument ('--language' , '-l' ,
84
- choices = languages .keys (),
85
- default = 'java' ,
86
- required = False ,
87
- help = argparse .SUPPRESS )
76
+ langs .add_argument ('--language' , '-l' , choices = languages .keys (),
77
+ default = 'java' , required = False , help = SUPPRESS )
88
78
for key , label in list (languages .items ()):
89
79
help = 'Set \' {}\' as the target programming language.' .format (label )
90
80
langs .add_argument ('--{}' .format (key ), action = 'store_true' , help = help )
91
81
92
82
# Extra arguments:
93
- extras = parser .add_argument_group ('Extra arguments' )
83
+ extras = p .add_argument_group ('Extra arguments' )
94
84
extras .add_argument ('--version' , '-v' , action = 'version' ,
95
- version = 'sklearn-porter v{}' .format (version ))
85
+ version = 'sklearn-porter v{}' .format (version ),
86
+ help = 'Show the version number and exit.' )
87
+ extras .add_argument ('--help' , '-h' , action = _HelpAction ,
88
+ help = "Show this help message and exit." )
96
89
97
90
# Show help by default:
98
91
if len (sys .argv ) == 1 :
99
- parser .print_help (sys .stderr )
92
+ p .print_help (sys .stderr )
100
93
sys .exit (1 )
101
94
102
95
# Return dictionary:
103
- args = vars (parser .parse_args (args ))
96
+ args = vars (p .parse_args (args ))
104
97
return args
105
98
106
99
@@ -126,7 +119,7 @@ def main():
126
119
break
127
120
128
121
# Define destination path:
129
- dest_dir = str (args .get ('output ' ))
122
+ dest_dir = str (args .get ('to ' ))
130
123
if dest_dir == '' or not isdir (dest_dir ):
131
124
dest_dir = pkl_file_path .split (sep )
132
125
del dest_dir [- 1 ]
0 commit comments