Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify command line options #650

Merged
merged 26 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ include(CPack)
add_subdirectory(data)
add_subdirectory(opl)
add_subdirectory(textscreen)
add_subdirectory(man)
add_subdirectory(src)
add_subdirectory(toolsrc)
add_subdirectory(setup)
add_subdirectory(man)
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
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
96 changes: 85 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,21 @@ 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 = (90 - indent),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiangreffrath Should we wrap help text for console output? Maybe we should write shorter help lines instead 😉

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help text should fit into one console line ideally. Do you have an example of a help text line that doesn't fit?

Copy link
Collaborator Author

@rfomin rfomin Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops missed your answer.
This is about a response to the -help command. I just generate our CMDLINE documentation, but with selected categories and parameters. So we have a lot of long lines here with multiple sentences: help.txt

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I like this very much! However, 90 chars is too long for a terminal, e.g. MSYS2's mintty defaults to 80x24. To achieve this, I think we shouldn't cut down the help strings. I very much prefer them to be real sentences and actually helpful. Could you tune it so that each line is broken after at most 80 chars?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, 80 columns is the standard, but I have noticed that many modern applications do not respect it.
Anyway, my English is bad, so I'll just let the Python textwrap library do its job 😄

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 +378,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 +531,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 +550,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 +577,31 @@ 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 \""

category = dict(categories)

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

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 +615,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 +649,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