-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold.cli.py
311 lines (238 loc) · 8.92 KB
/
old.cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
#!python3 -*- coding: utf-8 -*-
'''
Command line entry point for Veredi.
For usage:
$ ./veredi.py help
'''
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
# ---
# Python
# ---
import argparse
# ---
# Veredi: Misc
# ---
from veredi.log import logs
# ---
# Commands
# ---
# -----------------------------------------------------------------------------
# Constants
# -----------------------------------------------------------------------------
PARSER_DESC = (
"Runs inputs through Veredi and returns results."
)
# -----------------------------------------------------------------------------
# Script Helper Functions
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Parser Setup Functions
# -----------------------------------------------------------------------------
def _init_args(parser):
'''Adds arguments to ArgumentParser `parser`.
Args:
parser: argparse.ArgumentParser
Returns:
argparse.ArgumentParser with arguments added
'''
parser.add_argument(
'--verbose', '-v',
action='count',
default=0,
help=("Verbosity Level. Use multiple times for more verbose, "
"e.g. '-vvv'."))
parser.add_argument(
'--dry-run',
action='store_true',
help=('Set dry-run flag if script should not make any '
'changes to the saved data.'))
# parser.set_defaults(func=cmd_base)
# §-TODO-§ [2020-04-21]: repository pattern type
# e..g.? "file/yaml", "db/sqlite3"?
def _init_subcommands(parser):
'''Has all subcommands register themselves with the parser.
Args:
parser: argparse.ArgumentParser
Returns:
argparse.ArgumentParser with arguments added
'''
# §-TODO-§ [2020-04-21]: this?
subparsers = parser.add_subparsers(help='sub-command help')
# - title - title for the sub-parser group in help output; by default
# “subcommands” if description is provided, otherwise uses title
# for positional arguments
#
# - description - description for the sub-parser group in help output, by
# default None
#
# - prog - usage information that will be displayed with sub-command help,
# by default the name of the program and any positional arguments
# before the subparser argument
#
# - parser_class - Class which will be used to create sub-parser
# instances, by default the class of the current parser
# (e.g. ArgumentParser)
#
# - action - the basic type of action to be taken when this argument is
# encountered at the command line
#
# - dest - name of the attribute under which sub-command name will be
# stored; by default None and no value is stored
#
# - required - Whether or not a subcommand must be provided, by default
# False (added in 3.7)
#
# - help - help for sub-parser group in help output, by default None
#
# - metavar - string presenting available sub-commands in help; by default
# it is None and presents sub-commands in
# form {cmd1, cmd2, ..}
_init_cmd_roll(subparsers)
_init_cmd_session(subparsers)
def _init_cmd_roll(subparsers):
roll_parser = subparsers.add_parser(
'roll',
help=("Takes a dice expression (e.g. '3d20 + 12 + d6 - d10 / 2'), "
"rolls the dice, does the math, and returns the results."))
# Arg for type/system (e.g. d20)?
# §-TODO-§ [2020-04-26]: type/system?
roll_parser.add_argument('expression', nargs='*')
roll_parser.set_defaults(func=cmd_roll)
def _init_cmd_session(subparsers):
roll_parser = subparsers.add_parser(
'session',
help=("Loads a game's/session's data (players, etc). Then takes a "
"dice expression (e.g. '3d20 + $str_mod + d6 - d10 / 2'), "
"rolls the dice, does the math, and returns the results."))
# Arg for type/system (e.g. d20)?
# §-TODO-§ [2020-04-26]: type/system?
roll_parser.add_argument('-c', '--campaign',
type=str,
required=True,
help='Name of game/campaign.')
roll_parser.add_argument('-p', '--player',
type=str,
action='append',
nargs=2,
required=True,
metavar=('user-name', 'player-name'),
help='Name of game/campaign.')
roll_parser.add_argument('expression', nargs='*')
roll_parser.set_defaults(func=_not_implemented) # cmd_session)
def _not_implemented(*args, **kwargs):
raise NotImplementedError("_not_implemented called with:", args, kwargs)
def _init_parser():
'''Initializes argparse parser for this script.
Returns:
argparse.ArgumentParser ready to go
'''
parser = argparse.ArgumentParser(description=PARSER_DESC)
_init_args(parser)
_init_subcommands(parser)
return parser
def _parse_args(parser):
'''Parse command line input.
Args:
parser: argparse.ArgumentParser
Returns:
args (output of argparse.ArgumentParser.parse_args())
'''
return parser.parse_args()
# -----------------------------------------------------------------------------
# Other Setup Functions
# -----------------------------------------------------------------------------
def _init_logging():
log.init()
log.debug("test?")
# -----------------------------------------------------------------------------
# Sub-command Entry Points
# -----------------------------------------------------------------------------
# def cmd_base(args):
# print("Veredi has nothing to do...")
def cmd_roll(args):
from veredi.math.d20.parser import parse_input
expression = ' '.join(args.expression)
print("input: ", expression)
print("rolled:", parse_input(expression))
# def cmd_session(args):
# import veredi.game.session
# import veredi.repository.player
# # from veredi.roll.d20.parser import parse_input
# # ---
# # Repository Setup
# # ---
# root_data_dir = os.path.join(
# os.getcwd(),
# "..", # Test dir is sibling of veredi code dir
# "test",
# "data",
# "repository",
# "file",
# "json")
# root_human_dir = os.path.join(root_data_dir,
# "human")
# root_hashed_dir = os.path.join(root_data_dir,
# "hashed")
# repo_human = repository.player.PlayerRepository_FileJson(
# root_human_dir,
# repository.player.PathNameOption.HUMAN_SAFE)
# # ---
# # Session Setup
# # ---
# players = [("us1!{er", "jeff")]
# session = game.session.Session("some-forgotten-campaign",
# players,
# repo_human)
# # ---
# # Roll one thing and throw it all away! ^_^
# # ---
# expression = ' '.join(args.expression)
# print("input: ", expression)
# print("rolled:", session.roll("jeff", expression))
# -----------------------------------Veredi------------------------------------
# -- Main Command Line Entry Point --
# -----------------------------------------------------------------------------
if __name__ == '__main__':
# ---
# Setup
# ---
_init_logging()
# Setup parser and read command inputs into `_args`.
parser = _init_parser()
args = _parse_args(parser)
# ---
# Run
# ---
subcommand = getattr(args, 'func', None)
if subcommand is not None:
subcommand(args)
else:
# No subcommand found - print error message and help to stderr.
import sys
print("Sub-command not found.\n", file=sys.stderr)
# Examples:
print("Examples:")
print(' '.join([" ",
"doc-veredi python -m veredi",
"roll",
# dice expression
"d20 + 11"]))
print(' '.join([" ",
"doc-veredi python -m veredi",
"session",
# campaign name
"-c some-forgotten-campaign",
# player 1
"-p 'us1!{er' jeff",
# (can do more players...)
# dice expression
"d20 + '$str_mod'"]))
print("\n")
parser.print_help(sys.stderr)
sys.exit(1)
# print("-" * 10)
# print("Veredi dice roller:")
# print("-" * 10)
# user_data = input("roll> ")