From 6ac46c9708f46f9dd83dd36008d6f7c9569b74eb Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 28 Oct 2020 12:34:33 +1100 Subject: BLI_args: refactor argument passes Avoid passing the pass argument to BLI_argsAdd, instead set this once for each group of passes. This means we can add new passes without having to bump the arguments to BLI_argsAdd. --- source/blender/blenlib/BLI_args.h | 5 +- source/blender/blenlib/intern/BLI_args.c | 26 ++--- source/creator/creator_args.c | 176 ++++++++++++++----------------- 3 files changed, 98 insertions(+), 109 deletions(-) diff --git a/source/blender/blenlib/BLI_args.h b/source/blender/blenlib/BLI_args.h index 54b5161f15a..2bd0e7b9019 100644 --- a/source/blender/blenlib/BLI_args.h +++ b/source/blender/blenlib/BLI_args.h @@ -41,12 +41,14 @@ typedef int (*BA_ArgCallback)(int argc, const char **argv, void *data); struct bArgs *BLI_argsInit(int argc, const char **argv); void BLI_argsFree(struct bArgs *ba); +/** The pass to use for #BLI_argsAdd. */ +void BLI_argsPassSet(struct bArgs *ba, int current_pass); + /** * Pass starts at 1, -1 means valid all the time * short_arg or long_arg can be null to specify no short or long versions */ void BLI_argsAdd(struct bArgs *ba, - int pass, const char *short_arg, const char *long_arg, const char *doc, @@ -57,7 +59,6 @@ void BLI_argsAdd(struct bArgs *ba, * Short_case and long_case specify if those arguments are case specific */ void BLI_argsAddCase(struct bArgs *ba, - int pass, const char *short_arg, int short_case, const char *long_arg, diff --git a/source/blender/blenlib/intern/BLI_args.c b/source/blender/blenlib/intern/BLI_args.c index 91aabca7747..4a46bf73f78 100644 --- a/source/blender/blenlib/intern/BLI_args.c +++ b/source/blender/blenlib/intern/BLI_args.c @@ -63,6 +63,9 @@ struct bArgs { int argc; const char **argv; int *passes; + + /* Only use when initializing arguments. */ + int current_pass; }; static uint case_strhash(const void *ptr) @@ -128,6 +131,11 @@ void BLI_argsFree(struct bArgs *ba) MEM_freeN(ba); } +void BLI_argsPassSet(struct bArgs *ba, int current_pass) +{ + ba->current_pass = current_pass; +} + void BLI_argsPrint(struct bArgs *ba) { int i; @@ -163,14 +171,10 @@ static bArgDoc *internalDocs(struct bArgs *ba, return d; } -static void internalAdd(struct bArgs *ba, - const char *arg, - int pass, - int case_str, - BA_ArgCallback cb, - void *data, - bArgDoc *d) +static void internalAdd( + struct bArgs *ba, const char *arg, int case_str, BA_ArgCallback cb, void *data, bArgDoc *d) { + const int pass = ba->current_pass; bArgument *a; bAKey *key; @@ -204,7 +208,6 @@ static void internalAdd(struct bArgs *ba, } void BLI_argsAddCase(struct bArgs *ba, - int pass, const char *short_arg, int short_case, const char *long_arg, @@ -216,23 +219,22 @@ void BLI_argsAddCase(struct bArgs *ba, bArgDoc *d = internalDocs(ba, short_arg, long_arg, doc); if (short_arg) { - internalAdd(ba, short_arg, pass, short_case, cb, data, d); + internalAdd(ba, short_arg, short_case, cb, data, d); } if (long_arg) { - internalAdd(ba, long_arg, pass, long_case, cb, data, d); + internalAdd(ba, long_arg, long_case, cb, data, d); } } void BLI_argsAdd(struct bArgs *ba, - int pass, const char *short_arg, const char *long_arg, const char *doc, BA_ArgCallback cb, void *data) { - BLI_argsAddCase(ba, pass, short_arg, 0, long_arg, 0, doc, cb, data); + BLI_argsAddCase(ba, short_arg, 0, long_arg, 0, doc, cb, data); } static void internalDocPrint(bArgDoc *d) diff --git a/source/creator/creator_args.c b/source/creator/creator_args.c index 13e8885ae5d..d1f83b323a4 100644 --- a/source/creator/creator_args.c +++ b/source/creator/creator_args.c @@ -2053,59 +2053,59 @@ void main_args_setup(bContext *C, bArgs *ba) # define CB(a) a##_doc, a # define CB_EX(a, b) a##_doc_##b, a - // BLI_argsAdd(ba, pass, short_arg, long_arg, doc, cb, C); - /* end argument processing after -- */ - BLI_argsAdd(ba, -1, "--", NULL, CB(arg_handle_arguments_end), NULL); + BLI_argsPassSet(ba, -1); + BLI_argsAdd(ba, "--", NULL, CB(arg_handle_arguments_end), NULL); /* Pass 0: Environment Setup * * It's important these run before any initialization is done, since they set up * the environment used to access data-files, which are be used when initializing * sub-systems such as color management. */ - BLI_argsAdd( - ba, 0, NULL, "--python-use-system-env", CB(arg_handle_python_use_system_env_set), NULL); + BLI_argsPassSet(ba, 0); + BLI_argsAdd(ba, NULL, "--python-use-system-env", CB(arg_handle_python_use_system_env_set), NULL); /* Note that we could add used environment variables too. */ BLI_argsAdd( - ba, 0, NULL, "--env-system-datafiles", CB_EX(arg_handle_env_system_set, datafiles), NULL); - BLI_argsAdd( - ba, 0, NULL, "--env-system-scripts", CB_EX(arg_handle_env_system_set, scripts), NULL); - BLI_argsAdd(ba, 0, NULL, "--env-system-python", CB_EX(arg_handle_env_system_set, python), NULL); + ba, NULL, "--env-system-datafiles", CB_EX(arg_handle_env_system_set, datafiles), NULL); + BLI_argsAdd(ba, NULL, "--env-system-scripts", CB_EX(arg_handle_env_system_set, scripts), NULL); + BLI_argsAdd(ba, NULL, "--env-system-python", CB_EX(arg_handle_env_system_set, python), NULL); /* Pass 1: Background Mode & Settings * * Also and commands that exit after usage. */ - BLI_argsAdd(ba, 1, "-h", "--help", CB(arg_handle_print_help), ba); + BLI_argsPassSet(ba, 1); + BLI_argsAdd(ba, "-h", "--help", CB(arg_handle_print_help), ba); /* Windows only */ - BLI_argsAdd(ba, 1, "/?", NULL, CB_EX(arg_handle_print_help, win32), ba); + BLI_argsAdd(ba, "/?", NULL, CB_EX(arg_handle_print_help, win32), ba); - BLI_argsAdd(ba, 1, "-v", "--version", CB(arg_handle_print_version), NULL); + BLI_argsAdd(ba, "-v", "--version", CB(arg_handle_print_version), NULL); + BLI_argsAdd(ba, "-y", "--enable-autoexec", CB_EX(arg_handle_python_set, enable), (void *)true); BLI_argsAdd( - ba, 1, "-y", "--enable-autoexec", CB_EX(arg_handle_python_set, enable), (void *)true); - BLI_argsAdd( - ba, 1, "-Y", "--disable-autoexec", CB_EX(arg_handle_python_set, disable), (void *)false); + ba, "-Y", "--disable-autoexec", CB_EX(arg_handle_python_set, disable), (void *)false); - BLI_argsAdd(ba, 1, NULL, "--disable-crash-handler", CB(arg_handle_crash_handler_disable), NULL); - BLI_argsAdd(ba, 1, NULL, "--disable-abort-handler", CB(arg_handle_abort_handler_disable), NULL); + BLI_argsAdd(ba, NULL, "--disable-crash-handler", CB(arg_handle_crash_handler_disable), NULL); + BLI_argsAdd(ba, NULL, "--disable-abort-handler", CB(arg_handle_abort_handler_disable), NULL); - BLI_argsAdd(ba, 1, "-b", "--background", CB(arg_handle_background_mode_set), NULL); + BLI_argsAdd(ba, "-t", "--threads", CB(arg_handle_threads_set), NULL); - BLI_argsAdd(ba, 1, "-a", NULL, CB(arg_handle_playback_mode), NULL); + BLI_argsAdd(ba, "-b", "--background", CB(arg_handle_background_mode_set), NULL); - BLI_argsAdd(ba, 1, NULL, "--log", CB(arg_handle_log_set), ba); - BLI_argsAdd(ba, 1, NULL, "--log-level", CB(arg_handle_log_level_set), ba); - BLI_argsAdd(ba, 1, NULL, "--log-show-basename", CB(arg_handle_log_show_basename_set), ba); - BLI_argsAdd(ba, 1, NULL, "--log-show-backtrace", CB(arg_handle_log_show_backtrace_set), ba); - BLI_argsAdd(ba, 1, NULL, "--log-show-timestamp", CB(arg_handle_log_show_timestamp_set), ba); - BLI_argsAdd(ba, 1, NULL, "--log-file", CB(arg_handle_log_file_set), ba); + BLI_argsAdd(ba, "-a", NULL, CB(arg_handle_playback_mode), NULL); - BLI_argsAdd(ba, 1, "-d", "--debug", CB(arg_handle_debug_mode_set), ba); + BLI_argsAdd(ba, NULL, "--log", CB(arg_handle_log_set), ba); + BLI_argsAdd(ba, NULL, "--log-level", CB(arg_handle_log_level_set), ba); + BLI_argsAdd(ba, NULL, "--log-show-basename", CB(arg_handle_log_show_basename_set), ba); + BLI_argsAdd(ba, NULL, "--log-show-backtrace", CB(arg_handle_log_show_backtrace_set), ba); + BLI_argsAdd(ba, NULL, "--log-show-timestamp", CB(arg_handle_log_show_timestamp_set), ba); + BLI_argsAdd(ba, NULL, "--log-file", CB(arg_handle_log_file_set), ba); + + BLI_argsAdd(ba, "-d", "--debug", CB(arg_handle_debug_mode_set), ba); # ifdef WITH_FFMPEG BLI_argsAdd(ba, - 1, + NULL, "--debug-ffmpeg", CB_EX(arg_handle_debug_mode_generic_set, ffmpeg), @@ -2114,7 +2114,7 @@ void main_args_setup(bContext *C, bArgs *ba) # ifdef WITH_FREESTYLE BLI_argsAdd(ba, - 1, + NULL, "--debug-freestyle", CB_EX(arg_handle_debug_mode_generic_set, freestyle), @@ -2122,177 +2122,163 @@ void main_args_setup(bContext *C, bArgs *ba) # endif BLI_argsAdd(ba, - 1, + NULL, "--debug-python", CB_EX(arg_handle_debug_mode_generic_set, python), (void *)G_DEBUG_PYTHON); BLI_argsAdd(ba, - 1, + NULL, "--debug-events", CB_EX(arg_handle_debug_mode_generic_set, events), (void *)G_DEBUG_EVENTS); BLI_argsAdd(ba, - 1, + NULL, "--debug-handlers", CB_EX(arg_handle_debug_mode_generic_set, handlers), (void *)G_DEBUG_HANDLERS); BLI_argsAdd( - ba, 1, NULL, "--debug-wm", CB_EX(arg_handle_debug_mode_generic_set, wm), (void *)G_DEBUG_WM); + ba, NULL, "--debug-wm", CB_EX(arg_handle_debug_mode_generic_set, wm), (void *)G_DEBUG_WM); # ifdef WITH_XR_OPENXR BLI_argsAdd( - ba, 1, NULL, "--debug-xr", CB_EX(arg_handle_debug_mode_generic_set, xr), (void *)G_DEBUG_XR); + ba, NULL, "--debug-xr", CB_EX(arg_handle_debug_mode_generic_set, xr), (void *)G_DEBUG_XR); BLI_argsAdd(ba, - 1, + NULL, "--debug-xr-time", CB_EX(arg_handle_debug_mode_generic_set, xr_time), (void *)G_DEBUG_XR_TIME); # endif BLI_argsAdd(ba, - 1, NULL, "--debug-ghost", CB_EX(arg_handle_debug_mode_generic_set, handlers), (void *)G_DEBUG_GHOST); - BLI_argsAdd(ba, 1, NULL, "--debug-all", CB(arg_handle_debug_mode_all), NULL); + BLI_argsAdd(ba, NULL, "--debug-all", CB(arg_handle_debug_mode_all), NULL); - BLI_argsAdd(ba, 1, NULL, "--debug-io", CB(arg_handle_debug_mode_io), NULL); + BLI_argsAdd(ba, NULL, "--debug-io", CB(arg_handle_debug_mode_io), NULL); - BLI_argsAdd(ba, 1, NULL, "--debug-fpe", CB(arg_handle_debug_fpe_set), NULL); + BLI_argsAdd(ba, NULL, "--debug-fpe", CB(arg_handle_debug_fpe_set), NULL); # ifdef WITH_LIBMV - BLI_argsAdd(ba, 1, NULL, "--debug-libmv", CB(arg_handle_debug_mode_libmv), NULL); + BLI_argsAdd(ba, NULL, "--debug-libmv", CB(arg_handle_debug_mode_libmv), NULL); # endif # ifdef WITH_CYCLES_LOGGING - BLI_argsAdd(ba, 1, NULL, "--debug-cycles", CB(arg_handle_debug_mode_cycles), NULL); + BLI_argsAdd(ba, NULL, "--debug-cycles", CB(arg_handle_debug_mode_cycles), NULL); # endif - BLI_argsAdd(ba, 1, NULL, "--debug-memory", CB(arg_handle_debug_mode_memory_set), NULL); + BLI_argsAdd(ba, NULL, "--debug-memory", CB(arg_handle_debug_mode_memory_set), NULL); - BLI_argsAdd(ba, 1, NULL, "--debug-value", CB(arg_handle_debug_value_set), NULL); + BLI_argsAdd(ba, NULL, "--debug-value", CB(arg_handle_debug_value_set), NULL); BLI_argsAdd(ba, - 1, NULL, "--debug-jobs", CB_EX(arg_handle_debug_mode_generic_set, jobs), (void *)G_DEBUG_JOBS); + BLI_argsAdd( + ba, NULL, "--debug-gpu", CB_EX(arg_handle_debug_mode_generic_set, gpu), (void *)G_DEBUG_GPU); BLI_argsAdd(ba, - 1, - NULL, - "--debug-gpu", - CB_EX(arg_handle_debug_mode_generic_set, gpu), - (void *)G_DEBUG_GPU); - BLI_argsAdd(ba, - 1, NULL, "--debug-depsgraph", CB_EX(arg_handle_debug_mode_generic_set, depsgraph), (void *)G_DEBUG_DEPSGRAPH); BLI_argsAdd(ba, - 1, NULL, "--debug-depsgraph-build", CB_EX(arg_handle_debug_mode_generic_set, depsgraph_build), (void *)G_DEBUG_DEPSGRAPH_BUILD); BLI_argsAdd(ba, - 1, NULL, "--debug-depsgraph-eval", CB_EX(arg_handle_debug_mode_generic_set, depsgraph_eval), (void *)G_DEBUG_DEPSGRAPH_EVAL); BLI_argsAdd(ba, - 1, NULL, "--debug-depsgraph-tag", CB_EX(arg_handle_debug_mode_generic_set, depsgraph_tag), (void *)G_DEBUG_DEPSGRAPH_TAG); BLI_argsAdd(ba, - 1, NULL, "--debug-depsgraph-time", CB_EX(arg_handle_debug_mode_generic_set, depsgraph_time), (void *)G_DEBUG_DEPSGRAPH_TIME); BLI_argsAdd(ba, - 1, + NULL, "--debug-depsgraph-no-threads", CB_EX(arg_handle_debug_mode_generic_set, depsgraph_no_threads), (void *)G_DEBUG_DEPSGRAPH_NO_THREADS); BLI_argsAdd(ba, - 1, NULL, "--debug-depsgraph-pretty", CB_EX(arg_handle_debug_mode_generic_set, depsgraph_pretty), (void *)G_DEBUG_DEPSGRAPH_PRETTY); BLI_argsAdd(ba, - 1, NULL, "--debug-depsgraph-uuid", CB_EX(arg_handle_debug_mode_generic_set, depsgraph_build), (void *)G_DEBUG_DEPSGRAPH_UUID); BLI_argsAdd(ba, - 1, NULL, "--debug-gpumem", CB_EX(arg_handle_debug_mode_generic_set, gpumem), (void *)G_DEBUG_GPU_MEM); BLI_argsAdd(ba, - 1, NULL, "--debug-gpu-shaders", CB_EX(arg_handle_debug_mode_generic_set, gpumem), (void *)G_DEBUG_GPU_SHADERS); BLI_argsAdd(ba, - 1, NULL, "--debug-gpu-force-workarounds", CB_EX(arg_handle_debug_mode_generic_set, gpumem), (void *)G_DEBUG_GPU_FORCE_WORKAROUNDS); - BLI_argsAdd(ba, 1, NULL, "--debug-exit-on-error", CB(arg_handle_debug_exit_on_error), NULL); + BLI_argsAdd(ba, NULL, "--debug-exit-on-error", CB(arg_handle_debug_exit_on_error), NULL); - BLI_argsAdd(ba, 1, NULL, "--verbose", CB(arg_handle_verbosity_set), NULL); + BLI_argsAdd(ba, NULL, "--verbose", CB(arg_handle_verbosity_set), NULL); - BLI_argsAdd(ba, 1, NULL, "--app-template", CB(arg_handle_app_template), NULL); - BLI_argsAdd(ba, 1, NULL, "--factory-startup", CB(arg_handle_factory_startup_set), NULL); - BLI_argsAdd(ba, 1, NULL, "--enable-event-simulate", CB(arg_handle_enable_event_simulate), NULL); + BLI_argsAdd(ba, NULL, "--app-template", CB(arg_handle_app_template), NULL); + BLI_argsAdd(ba, NULL, "--factory-startup", CB(arg_handle_factory_startup_set), NULL); + BLI_argsAdd(ba, NULL, "--enable-event-simulate", CB(arg_handle_enable_event_simulate), NULL); /* Pass 2: Custom Window Stuff. */ - BLI_argsAdd(ba, 2, "-p", "--window-geometry", CB(arg_handle_window_geometry), NULL); - BLI_argsAdd(ba, 2, "-w", "--window-border", CB(arg_handle_with_borders), NULL); - BLI_argsAdd(ba, 2, "-W", "--window-fullscreen", CB(arg_handle_without_borders), NULL); - BLI_argsAdd(ba, 2, "-M", "--window-maximized", CB(arg_handle_window_maximized), NULL); - BLI_argsAdd(ba, 2, NULL, "--no-window-focus", CB(arg_handle_no_window_focus), NULL); - BLI_argsAdd(ba, 2, "-con", "--start-console", CB(arg_handle_start_with_console), NULL); - BLI_argsAdd(ba, 2, "-R", NULL, CB(arg_handle_register_extension), NULL); - BLI_argsAdd(ba, 2, "-r", NULL, CB_EX(arg_handle_register_extension, silent), ba); - BLI_argsAdd(ba, 2, NULL, "--no-native-pixels", CB(arg_handle_native_pixels_set), ba); + BLI_argsPassSet(ba, 2); + BLI_argsAdd(ba, "-p", "--window-geometry", CB(arg_handle_window_geometry), NULL); + BLI_argsAdd(ba, "-w", "--window-border", CB(arg_handle_with_borders), NULL); + BLI_argsAdd(ba, "-W", "--window-fullscreen", CB(arg_handle_without_borders), NULL); + BLI_argsAdd(ba, "-M", "--window-maximized", CB(arg_handle_window_maximized), NULL); + BLI_argsAdd(ba, NULL, "--no-window-focus", CB(arg_handle_no_window_focus), NULL); + BLI_argsAdd(ba, "-con", "--start-console", CB(arg_handle_start_with_console), NULL); + BLI_argsAdd(ba, "-R", NULL, CB(arg_handle_register_extension), NULL); + BLI_argsAdd(ba, "-r", NULL, CB_EX(arg_handle_register_extension, silent), ba); + BLI_argsAdd(ba, NULL, "--no-native-pixels", CB(arg_handle_native_pixels_set), ba); /* Pass 3: Disabling Things & Forcing Settings. */ - BLI_argsAddCase(ba, 3, "-noaudio", 1, NULL, 0, CB(arg_handle_audio_disable), NULL); - BLI_argsAddCase(ba, 3, "-setaudio", 1, NULL, 0, CB(arg_handle_audio_set), NULL); + BLI_argsPassSet(ba, 3); + BLI_argsAddCase(ba, "-noaudio", 1, NULL, 0, CB(arg_handle_audio_disable), NULL); + BLI_argsAddCase(ba, "-setaudio", 1, NULL, 0, CB(arg_handle_audio_set), NULL); /* Pass 4: Processing Arguments. */ - BLI_argsAdd(ba, 4, "-f", "--render-frame", CB(arg_handle_render_frame), C); - BLI_argsAdd(ba, 4, "-a", "--render-anim", CB(arg_handle_render_animation), C); - BLI_argsAdd(ba, 4, "-S", "--scene", CB(arg_handle_scene_set), C); - BLI_argsAdd(ba, 4, "-s", "--frame-start", CB(arg_handle_frame_start_set), C); - BLI_argsAdd(ba, 4, "-e", "--frame-end", CB(arg_handle_frame_end_set), C); - BLI_argsAdd(ba, 4, "-j", "--frame-jump", CB(arg_handle_frame_skip_set), C); - BLI_argsAdd(ba, 4, "-P", "--python", CB(arg_handle_python_file_run), C); - BLI_argsAdd(ba, 4, NULL, "--python-text", CB(arg_handle_python_text_run), C); - BLI_argsAdd(ba, 4, NULL, "--python-expr", CB(arg_handle_python_expr_run), C); - BLI_argsAdd(ba, 4, NULL, "--python-console", CB(arg_handle_python_console_run), C); - BLI_argsAdd(ba, 4, NULL, "--python-exit-code", CB(arg_handle_python_exit_code_set), NULL); - BLI_argsAdd(ba, 4, NULL, "--addons", CB(arg_handle_addons_set), C); - - BLI_argsAdd(ba, 4, "-o", "--render-output", CB(arg_handle_output_set), C); - BLI_argsAdd(ba, 4, "-E", "--engine", CB(arg_handle_engine_set), C); - - BLI_argsAdd(ba, 4, "-F", "--render-format", CB(arg_handle_image_type_set), C); - BLI_argsAdd(ba, 1, "-t", "--threads", CB(arg_handle_threads_set), NULL); - BLI_argsAdd(ba, 4, "-x", "--use-extension", CB(arg_handle_extension_set), C); + BLI_argsPassSet(ba, 4); + BLI_argsAdd(ba, "-f", "--render-frame", CB(arg_handle_render_frame), C); + BLI_argsAdd(ba, "-a", "--render-anim", CB(arg_handle_render_animation), C); + BLI_argsAdd(ba, "-S", "--scene", CB(arg_handle_scene_set), C); + BLI_argsAdd(ba, "-s", "--frame-start", CB(arg_handle_frame_start_set), C); + BLI_argsAdd(ba, "-e", "--frame-end", CB(arg_handle_frame_end_set), C); + BLI_argsAdd(ba, "-j", "--frame-jump", CB(arg_handle_frame_skip_set), C); + BLI_argsAdd(ba, "-P", "--python", CB(arg_handle_python_file_run), C); + BLI_argsAdd(ba, NULL, "--python-text", CB(arg_handle_python_text_run), C); + BLI_argsAdd(ba, NULL, "--python-expr", CB(arg_handle_python_expr_run), C); + BLI_argsAdd(ba, NULL, "--python-console", CB(arg_handle_python_console_run), C); + BLI_argsAdd(ba, NULL, "--python-exit-code", CB(arg_handle_python_exit_code_set), NULL); + BLI_argsAdd(ba, NULL, "--addons", CB(arg_handle_addons_set), C); + + BLI_argsAdd(ba, "-o", "--render-output", CB(arg_handle_output_set), C); + BLI_argsAdd(ba, "-E", "--engine", CB(arg_handle_engine_set), C); + + BLI_argsAdd(ba, "-F", "--render-format", CB(arg_handle_image_type_set), C); + BLI_argsAdd(ba, "-x", "--use-extension", CB(arg_handle_extension_set), C); # undef CB # undef CB_EX -- cgit v1.2.3