argparse
ensembl.utils.argparse
¶
Provide an extended version of argparse.ArgumentParser
with additional functionality.
Examples:
>>> from pathlib import Path
>>> from ensembl.util.argparse import ArgumentParser
>>> parser = ArgumentParser(description="Tool description")
>>> parser.add_argument_src_path("--src_file", required=True, help="Path to source file")
>>> parser.add_server_arguments(help="Server to connect to")
>>> args = parser.parse_args()
>>> args
Namespace(host='localhost', port=3826, src_file=PosixPath('/path/to/src_file.txt'),
url=URL('mysql://username@localhost:3826'), user='username')
ArgumentError
¶
Bases: Exception
An error from creating an argument (optional or positional).
Source code in src/ensembl/utils/argparse.py
49 50 |
|
ArgumentParser
¶
Bases: ArgumentParser
Extends argparse.ArgumentParser
with additional methods and functionality.
The default behaviour of the help text will be to display the default values on every non-required
argument, i.e. optional arguments with required=False
.
Source code in src/ensembl/utils/argparse.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter
instance-attribute
¶
add_argument(*args, **kwargs)
¶
Extends the parent function by excluding the default value in the help text when not provided.
Only applied to required arguments without a default value, i.e. positional arguments or optional
arguments with required=True
.
Source code in src/ensembl/utils/argparse.py
132 133 134 135 136 137 138 139 140 141 |
|
add_argument_dst_path(*args, exists_ok=True, **kwargs)
¶
Adds pathlib.Path
argument, checking if it is writable at parsing time.
If "metavar" is not defined it is added with "PATH" as value to improve help text readability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exists_ok
|
bool
|
Do not raise an error if the destination path already exists. |
True
|
Source code in src/ensembl/utils/argparse.py
153 154 155 156 157 158 159 160 161 162 163 164 |
|
add_argument_src_path(*args, **kwargs)
¶
Adds pathlib.Path
argument, checking if it exists and it is readable at parsing time.
If "metavar" is not defined, it is added with "PATH" as value to improve help text readability.
Source code in src/ensembl/utils/argparse.py
143 144 145 146 147 148 149 150 151 |
|
add_argument_url(*args, **kwargs)
¶
Adds sqlalchemy.engine.URL
argument.
If "metavar" is not defined it is added with "URI" as value to improve help text readability.
Source code in src/ensembl/utils/argparse.py
166 167 168 169 170 171 172 173 174 |
|
add_log_arguments(add_log_file=False)
¶
Adds the usual set of arguments required to set and initialise a logging system.
The current set includes a mutually exclusive group for the default logging level: --verbose
,
--debug
or --log LEVEL
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
add_log_file
|
bool
|
Add arguments to allow storing messages into a file, i.e. |
False
|
Source code in src/ensembl/utils/argparse.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
add_numeric_argument(*args, type=float, min_value=None, max_value=None, **kwargs)
¶
Adds a numeric argument with constrains on its type and its minimum or maximum value.
Note that the default value (if defined) is not checked unless the argument is an optional argument and no value is provided in the command line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type
|
Callable[[str], int | float]
|
Type to convert the argument value to when parsing. |
float
|
min_value
|
int | float | None
|
Minimum value constrain. If |
None
|
max_value
|
int | float | None
|
Maximum value constrain. If |
None
|
Source code in src/ensembl/utils/argparse.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
add_server_arguments(prefix='', include_database=False, help=None)
¶
Adds the usual set of arguments needed to connect to a server, i.e. --host
, --port
, --user
and --password
(optional).
Note that the parser will assume this is a MySQL server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
Prefix to add the each argument, e.g. if prefix is |
''
|
include_database
|
bool
|
Include |
False
|
help
|
str | None
|
Description message to include for this set of arguments. |
None
|
Source code in src/ensembl/utils/argparse.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
parse_args(*args, **kwargs)
¶
Extends the parent function by adding a new URL argument for every server group added.
The type of this new argument will be sqlalchemy.engine.URL
. It also logs all the parsed
arguments for debugging purposes when logging arguments have been added.
Source code in src/ensembl/utils/argparse.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|