Index

lognestmonster / 23adbb8

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
7928 Aug 2019 17:32ea40fffUpdate argument parsingJosh Stockin1225N

Blob @ lognestmonster / parser / parseargs.py

application/x-python2319 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 format import *
21from args import *
22# yeah, I know argparse is a thing, but I don't like it...
23
24def get_arg_from_indicator(indicator):
25 for option in ARGUMENT_OPTIONS:
26 if indicator in ARGUMENT_OPTIONS[option]["indicators"]:
27 return option
28 return None
29
30def get_arg_valcount(name):
31 return ARGUMENT_OPTIONS[name]["valcount"]
32
33def parseargs(argv):
34 options = {}
35 argn = 0
36 while argn < len(argv):
37 arg = argv[argn]
38 if arg == "-":
39 if argn == len(argv)-1: # if it's the last argument, allow it
40 break
41 opt = get_arg_from_indicator(arg)
42 if argn is not len(argv) - 1 and opt is None: # is a flag/option but is not the Queue
43 options = TEXT_RED + "error:" + RESET + " unknown flag/option '" + arg + "'"
44 return options
45 if opt in options:
46 options = TEXT_RED + "error:" + RESET + " double definition of argument '" + arg + "'"
47 return options
48 if opt is not None: # opt _can_ be None due to the last argument being Queue
49 valcount = get_arg_valcount(opt)
50 if valcount > 0:
51 options[opt] = []
52 for i in range(1, valcount+1):
53 if argn+1 < len(argv) - 1 and argv[argn+1].startswith("-") is not True:
54 options[opt].append(argv[argn + 1])
55 argn += 1
56 else:
57 options = TEXT_RED + "error:" + RESET + " argument '" + arg + "' requires " + \
58 str(valcount) + " " + (valcount is 1 and "value" or "values") + \
59 ", only " + str(i-1) + " " + (i-1 is 1 and "was" or "were") + " given"
60 return options
61 else:
62 options[opt] = None
63 argn += 1
64 return options
65