Index

lognestmonster / 9483e1f

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
9508 Sep 2019 15:57c896833block parsing optimization, argument feedback updatesJosh Stockin134N

Blob @ lognestmonster / parser / parseargs.py

application/x-python2205 bytesdownload raw
1# lognestmonster Copyright (c) 2019 Joshua 'joshuas3' Stockin
2# <https://github.com/JoshuaS3/lognestmonster/>.
3
4
5# This file is part of lognestmonster.
6
7# lognestmonster is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# lognestmonster is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with lognestmonster. If not, see <https://www.gnu.org/licenses/>.
19
20from args import *
21# yeah, I know argparse is a thing, but I don't like it...
22
23def get_arg_from_indicator(indicator):
24 for option in ARGUMENT_OPTIONS:
25 if indicator in ARGUMENT_OPTIONS[option]["indicators"]:
26 return option
27 return None
28
29def get_arg_valcount(name):
30 return ARGUMENT_OPTIONS[name]["valcount"]
31
32def parseargs(argv):
33 options = {}
34 argn = 0
35 while argn < len(argv):
36 arg = argv[argn]
37 if arg == "-":
38 if argn == len(argv)-1: # if it's the last argument, allow it
39 break
40 opt = get_arg_from_indicator(arg)
41 if argn is not len(argv) - 1 and opt is None: # is a flag/option but is not the Queue
42 options = "unknown flag/option '" + arg + "'"
43 return options
44 if opt in options:
45 options = "double definition of argument '" + arg + "'"
46 return options
47 if opt is not None: # opt _can_ be None due to the last argument being Queue
48 valcount = get_arg_valcount(opt)
49 if valcount > 0:
50 options[opt] = []
51 for i in range(1, valcount+1):
52 if argn+1 < len(argv) - 1 and argv[argn+1].startswith("-") is not True:
53 options[opt].append(argv[argn + 1])
54 argn += 1
55 else:
56 options = "argument '" + arg + "' requires " + \
57 str(valcount) + " " + (valcount is 1 and "value" or "values") + \
58 ", only " + str(i-1) + " " + (i-1 is 1 and "was" or "were") + " given"
59 return options
60 else:
61 options[opt] = None
62 argn += 1
63 return options
64