Skip to content

Commit

Permalink
verify command line options (#650)
Browse files Browse the repository at this point in the history
* verify command line options

* fix cmake

* add -deh -bex to multiple options check

* fix typo

* fix parameter count check

* add validation for some arguments

* fix skill 0

* validate all parameters with argumets

* cosmetic fixes

* don't check undocumented options if -devparm is set

* fix format error

* fix whitespace

* docgen cleanup

* rename GenParams->paramsgen

* fix -turbo check

* more docgen cleanup

* generate help string

* use dictionary

* condense help text

* escape `"`

* remove -gameversion from help string

* set wrap width to 80, few tweaks

* remove debug line

* -setmem error message correction

* fix -episode check and -skill description

* little cleanup
  • Loading branch information
rfomin authored Jul 14, 2022
1 parent d8a068c commit 399e88c
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 58 deletions.
10 changes: 10 additions & 0 deletions man/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

if(Python3_EXECUTABLE)
add_custom_target(paramsgen

This comment has been minimized.

Copy link
@fabiangreffrath

fabiangreffrath Aug 24, 2022

Owner

Can we somehow only call this if one of the src/*.c source files have changed? The way it currently works, it regenerates params.h each time and thus rebuilds m_argv.c.o and links the woof executable every second build.

This comment has been minimized.

Copy link
@rfomin

rfomin Aug 25, 2022

Author Collaborator

I didn't manage to do it with CMake. We can add params.h to the repository and just edit it manually.

COMMAND "${Python3_EXECUTABLE}" docgen -a
"${PROJECT_SOURCE_DIR}/src" > "${PROJECT_SOURCE_DIR}/src/params.h"
DEPENDS "${PROJECT_SOURCE_DIR}/src"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
endif()

configure_file(WoofInstall.cmake.in WoofInstall.cmake ESCAPE_QUOTES @ONLY)
install(SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/WoofInstall.cmake")

Expand Down
98 changes: 87 additions & 11 deletions man/docgen
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,11 @@ import os
import re
import glob
import getopt
import textwrap

TEXT_WRAP_WIDTH = 78
INCLUDE_STATEMENT_RE = re.compile("@include\s+(\S+)")

# Use appropriate stdout function for Python 2 or 3

def stdout(buf):
if sys.version_info.major < 3:
sys.stdout.write(buf)
else:
sys.stdout.buffer.write(buf)

# Find the maximum width of a list of parameters (for plain text output)

def parameter_list_width(params):
Expand Down Expand Up @@ -116,6 +109,26 @@ class Category:

return result

def help_output(self):
result = self.description + ": \n"

self.params.sort()

params_help = []

for p in self.params:
if p.should_show() and p.help:
params_help.append(p)

w = parameter_list_width(params_help)

for p in params_help:
result += p.help_output(w)

result = result.rstrip() + "\n"

return result

def markdown_output(self):
result = "## %s\n\n| Parameter | Description |\n| - | - |\n" % self.description

Expand All @@ -142,6 +155,17 @@ class Category:

return result

def carray_output(self, check_args):
result = ""

self.params.sort()

for p in self.params:
if p.should_show() and p.args if check_args else not p.args:
result += "\"" + p.carray_output() + "\",\n"

return result

def manpage_output(self):
result = ".SH " + self.description.upper() + "\n"

Expand Down Expand Up @@ -199,6 +223,7 @@ class Parameter:
self.args = None
self.platform = None
self.category = None
self.help = False
self.vanilla_option = False
self.games = None

Expand All @@ -223,6 +248,8 @@ class Parameter:
self.platform = data
elif option_type == "category":
self.category = data
elif option_type == "help":
self.help = True
elif option_type == "vanilla":
self.vanilla_option = True
elif option_type == "game":
Expand Down Expand Up @@ -275,6 +302,20 @@ class Parameter:

return result

def help_output(self, indent):
result = self.name

if self.args:
result += " " + self.args

result += " " * (indent - len(result))

result += textwrap.fill(self.text, width = 80 - indent,
subsequent_indent = " " * indent)
result += "\n"

return result

def markdown_output(self):
if self.args:
name = "%s %s" % (self.name, self.args)
Expand Down Expand Up @@ -336,6 +377,12 @@ class Parameter:

return result

def carray_output(self):

result = self.name

return result

# Read list of wiki pages

def read_wikipages():
Expand Down Expand Up @@ -483,7 +530,6 @@ def print_template(template_file, substs, content):
for k,v in substs.items():
line = line.replace(k,v)
print(line.rstrip())
# stdout(line.rstrip().encode('UTF-8') + b'\n')
finally:
f.close()

Expand All @@ -503,7 +549,6 @@ def wiki_output(targets, _, template):

for t in targets:
print(st.wiki_output())
# stdout(t.wiki_output().encode('UTF-8') + b'\n')

def markdown_output(targets, substs, template_file):
content = ""
Expand Down Expand Up @@ -531,6 +576,34 @@ def completion_output(targets, substs, template_file):

print_template(template_file, substs, content)

def carray_output(targets, substs, template):

content = "\nstatic const char *params[] = {\n"

for t in targets:
content += t.carray_output(False)

content += "};\n\nstatic const char *params_with_args[] = {\n"

for t in targets:
content += t.carray_output(True)

content += "};\n\n#define HELP_STRING \""

c = dict(categories)

for t in targets:
# no video and obscure category
if t != c["video"] and t != c["obscure"]:
s = t.help_output() + "\n";
s = s.replace("\"", "\\\"")
s = s.replace("\n", "\\n\\\n")
content += s

content += "\""

print(content)

def usage():
print("Usage: %s [-V] [-c tag] -s project_name [ -z shortname ] ( -M | -m | -w | -p ) <dir>..." \
% sys.argv[0])
Expand All @@ -544,13 +617,14 @@ def usage():
print(" -w : Wikitext output")
print(" -p : Plaintext output")
print(" -b : Bash-Completion output")
print(" -a : C array output")
print(" -V : Don't show Vanilla Doom options")
print(" -g : Only document options for specified game.")
sys.exit(0)

# Parse command line

opts, args = getopt.getopt(sys.argv[1:], "s:z:M:m:wp:b:c:g:V")
opts, args = getopt.getopt(sys.argv[1:], "s:z:M:m:wp:b:ap:c:g:V")

output_function = None
template = None
Expand All @@ -577,6 +651,8 @@ for opt in opts:
elif opt[0] == "-b":
output_function = completion_output
template = opt[1]
elif opt[0] == "-a":
output_function = carray_output
elif opt[0] == "-V":
show_vanilla_options = False
elif opt[0] == "-c":
Expand Down
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
params.h
9 changes: 6 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ set(WOOF_SOURCES
z_zone.c z_zone.h
../miniz/miniz.c ../miniz/miniz.h)

list(APPEND WOOF_LIBRARIES textscreen)

if(WIN32)
list(APPEND
WOOF_SOURCES
Expand Down Expand Up @@ -156,13 +154,18 @@ add_executable(woof WIN32 ${WOOF_SOURCES})
target_woof_settings(woof)
target_include_directories(woof PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../")

if(Python3_EXECUTABLE)
add_dependencies(woof paramsgen)
target_compile_definitions(woof PRIVATE HAVE_PARAMS_GEN)
endif()

if (FluidSynth_FOUND)
list(APPEND WOOF_LIBRARIES FluidSynth::FluidSynth)
target_compile_definitions(woof PRIVATE HAVE_FLUIDSYNTH)
endif()

target_link_libraries(woof PRIVATE ${WOOF_LIBRARIES}
SDL2::SDL2 SDL2::mixer SDL2::net opl)
SDL2::SDL2 SDL2::mixer SDL2::net opl textscreen)

if(MSVC)
# MSVC tries to supply a default manifest and complains when it finds ours
Expand Down
1 change: 1 addition & 0 deletions src/d_iwad.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ char *D_FindIWADFile(GameMode_t *mode, GameMission_t *mission)
// Specify an IWAD file to use.
//
// @arg <file>
// @help
//

int iwadparm = M_CheckParmWithArgs("-iwad", 1);
Expand Down
7 changes: 5 additions & 2 deletions src/d_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ void D_StartNetGame(net_gamesettings_t *settings,
i = M_CheckParmWithArgs("-extratics", 1);

if (i > 0)
settings->extratics = atoi(myargv[i+1]);
settings->extratics = M_ParmArgToInt(i);
else
settings->extratics = 1;

Expand All @@ -371,7 +371,7 @@ void D_StartNetGame(net_gamesettings_t *settings,
i = M_CheckParmWithArgs("-dup", 1);

if (i > 0)
settings->ticdup = atoi(myargv[i+1]);
settings->ticdup = M_ParmArgToInt(i);
else
settings->ticdup = 1;

Expand Down Expand Up @@ -433,6 +433,7 @@ boolean D_InitNetGame(net_connect_data_t *connect_data)

//!
// @category net
// @help
//
// Start a multiplayer server, listening for connections.
//
Expand All @@ -453,6 +454,7 @@ boolean D_InitNetGame(net_connect_data_t *connect_data)
{
//!
// @category net
// @help
//
// Automatically search the local LAN for a multiplayer
// server and join it.
Expand All @@ -473,6 +475,7 @@ boolean D_InitNetGame(net_connect_data_t *connect_data)
//!
// @arg <address>
// @category net
// @help
//
// Connect to a multiplayer server running on the given
// address.
Expand Down
Loading

0 comments on commit 399e88c

Please sign in to comment.