Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Nathan/Eilisha Shiraini
colorcode
Commits
00e4f039
Commit
00e4f039
authored
Sep 26, 2018
by
Nathan/Eilisha Shiraini
Browse files
Created the argument parser
parent
3ad6be2e
Changes
1
Hide whitespace changes
Inline
Side-by-side
colorcode/cli.py
0 → 100644
View file @
00e4f039
"""
Command line interface
This module contains the whole command line interface
including argument parsing and main procedure
"""
import
argparse
from
.loader
import
BUILDERS
from
.imgen.interleave
import
INTERLEAVING_MODES
_parseBool
=
lambda
i
:
i
.
lower
()[
0
]
in
'1tyo'
and
i
.
lower
()[
1
]
!=
'f'
def
_apInit
():
parser
=
argparse
.
ArgumentParser
(
description
=
"Generate multi-colored 2D codes"
)
parser
.
add_argument
(
'-t'
,
'--type'
,
action
=
'store'
,
choices
=
BUILDERS
.
keys
(),
default
=
'aztec'
,
dest
=
'type'
,
help
=
'Type of code to generate'
,
)
parser
.
add_argument
(
'-i'
,
'--interleaving'
,
action
=
'store'
,
choices
=
INTERLEAVING_MODES
.
keys
(),
default
=
None
,
dest
=
'int'
,
help
=
'Interleaving method to use'
,
)
parser
.
add_argument
(
'-m'
,
'--module-size'
,
action
=
'store'
,
type
=
int
,
default
=
32
,
dest
=
'msize'
,
help
=
'Number of pixels on one side of a module'
,
)
parser
.
add_argument
(
'-q'
,
'--quiet-zone'
,
action
=
'store'
,
type
=
int
,
default
=
4
,
dest
=
'qzone'
,
help
=
'Number of modules between the edge of the image and the code'
,
)
parser
.
add_argument
(
'--aztec-layers'
,
action
=
'store'
,
type
=
int
,
default
=
None
,
dest
=
'aztec_layers'
,
help
=
'For Aztec codes: force the number of layers'
,
)
parser
.
add_argument
(
'--aztec-compact'
,
action
=
'store'
,
type
=
_parseBool
,
default
=
None
,
dest
=
'aztec_compact'
,
help
=
'For Aztec codes: force the use of compact mode (truthy) or full mode (falsey)'
,
)
parser
.
add_argument
(
'-f'
,
'--from-files'
,
action
=
'store_true'
,
dest
=
'usefiles'
,
help
=
'If specified, the DATA arguments will be interpeted as file names from which the data will be read'
,
)
parser
.
add_argument
(
'-o'
,
'--out'
,
action
=
'store'
,
default
=
'code.png'
,
dest
=
'out'
,
help
=
'If specified, the DATA arguments will be interpeted as file names from which the data will be read'
,
)
parser
.
add_argument
(
'data'
,
action
=
'store'
,
nargs
=
'+'
,
metavar
=
'DATA'
,
dest
=
'data'
,
help
=
'The data of each code'
,
)
return
parser
def
parseCLI
():
"""
Parse command line arguments
This function parse the command line arguments
with which the program has been started
and prepares them for use with the main program
Returns
-------
data : :py:class:`list`
The list of data elements to encode
cgenKwargs : :py:class:`dict`
The dictionnary to be passed by keyword argument expansion
to the code builder constructor
imgenKwargs : :py:class:`dict`
The dictionnary to be passed by keyword argument expansion
to the image builder constructor
"""
args
=
_apInit
().
parse_args
()
codeType
=
args
.
type
interleave
=
INTERLEAVING_MODES
[
args
.
int
]
if
args
.
usefiles
:
data
=
[
open
(
f
,
'rb'
)
for
f
in
args
.
data
]
else
:
data
=
[
d
.
encode
(
'utf8'
)
for
d
in
args
.
data
]
if
len
(
data
)
>
interleave
[
'maxcodes'
]:
raise
ArgumentError
(
"Too many codes specified for this interleaving mode"
)
imgenKwargs
=
{
'interleaving'
:
interleave
[
'int'
],
'imageMode'
:
interleave
[
'mode'
],
'modSize'
:
args
.
msize
,
'quietZone'
:
args
.
qzone
,
}
cgenKwargs
=
{}
if
codeType
==
'aztec'
:
cgenKwargs
[
'forceLayers'
]
=
args
.
aztec_layers
cgenKwargs
[
'forceCompact'
]
=
args
.
aztec_compact
return
data
,
cgenKwargs
,
imgenKwargs
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment