Make sure that launch options be documented (with --help)
authorhgn <hgodden00@gmail.com>
Thu, 12 Dec 2024 16:34:51 +0000 (16:34 +0000)
committerhgn <hgodden00@gmail.com>
Thu, 12 Dec 2024 16:34:51 +0000 (16:34 +0000)
vg_engine.c
vg_opt.c
vg_opt.h

index 98a43440edfbdc3ce31d6c51d33e315e073811b7..b07193dfeaa3de13bb0fc95ad8b2601ac8d82642 100644 (file)
@@ -524,22 +524,28 @@ static void _vg_process_launch_opts_internal( int argc, char *argv[] )
    char *arg;
    while( vg_argp( argc, argv ) )
    {
-      if( (arg = vg_opt_arg( 'w' )) )
+      if( (arg = vg_opt_arg( 'w', "Render output width" )) )
          vg.window_x = atoi( arg );
 
-      if( (arg = vg_opt_arg( 'h' )) )
+      if( (arg = vg_opt_arg( 'h', "Render output height" )) )
          vg.window_y = atoi( arg );
 
-      if( (arg = vg_long_opt_arg( "samples" )) )
+      if( (arg = vg_long_opt_arg( "samples", "Rendering samples per pixel" )) )
          vg.samples = VG_MAX( 0, VG_MIN( 8, atoi( arg ) ) );
 
-      if(  vg_long_opt( "use-libc-malloc" ) )
+      if(  vg_long_opt( "use-libc-malloc", "Use standard libc allocator" ) )
          vg_mem.use_libc_malloc = 1;
 
-      if( vg_long_opt( "high-performance" ) )
+      if( vg_long_opt( "high-performance", "Turn graphics to lowest quality" ) )
          vg.quality_profile = k_quality_profile_low;
 
       vg_launch_opt();
+
+      if( vg_long_opt( "help", "Helps you" ) )
+      {
+         _vg_print_options();
+         exit(0);
+      }
    }
 }
 
index ba0f9c4cb18982e17408c6d85320fd19f4dc631c..80b314678666e660500fa03b771f5ef338c35d85 100644 (file)
--- a/vg_opt.c
+++ b/vg_opt.c
@@ -13,6 +13,7 @@
  *   short options     |  -a value
  *   multi-set options |  -ab value
  *
+ *   long gnu flag     |  --long-opt
  *   long gnu options  |  --long-value=test
  *   standard agument  |  regular_thing
  */
@@ -23,6 +24,88 @@ static int vg_argc = 0;
 static int vg_consume_next = 0;
 static char **vg_argv;
 
+enum vg_opt_type
+{
+   k_vg_opt_type_standard,
+   k_vg_opt_type_flag,
+   k_vg_opt_type_arg,
+   k_vg_opt_type_long_flag,
+   k_vg_opt_type_long_arg
+};
+
+struct _vg_opt_reg
+{
+   const char *alias, *desc;
+   char alias_c;
+   u8 type;
+}
+static _vg_all_opts[32];
+static u32 _vg_opt_count = 0;
+
+static void _vg_opt_observe( const char *alias, char alias_c, const char *desc,
+                             enum vg_opt_type type )
+{
+   for( u32 i=0; i<_vg_opt_count; i ++ )
+   {
+      struct _vg_opt_reg *reg = &_vg_all_opts[i];
+      if( (type == k_vg_opt_type_flag || type == k_vg_opt_type_arg) 
+            && reg->alias_c == alias_c )
+      {
+         return;
+      }
+      
+      if( (type == k_vg_opt_type_long_flag || type == k_vg_opt_type_long_arg) 
+            && reg->alias == alias )
+      {
+         return;
+      }
+
+      if( (type == k_vg_opt_type_standard) 
+            && (reg->alias == NULL && reg->alias_c == 0) )
+      {
+         return;
+      }
+   }
+
+   if( _vg_opt_count < VG_ARRAY_LEN( _vg_all_opts ) )
+   {
+      struct _vg_opt_reg *reg = &_vg_all_opts[ _vg_opt_count ++ ];
+      reg->alias = alias;
+      reg->alias_c = alias_c;
+      reg->desc = desc;
+      reg->type = type;
+   }
+}
+
+void _vg_print_options(void)
+{
+   for( u32 i=0; i<_vg_opt_count; i ++ )
+   {
+      struct _vg_opt_reg *reg = &_vg_all_opts[i];
+
+      if( reg->type == k_vg_opt_type_flag )
+         printf( "-%c                      %s\n", reg->alias_c, reg->desc );
+
+      if( reg->type == k_vg_opt_type_arg )
+         printf( "-%c <value>              %s\n", reg->alias_c, reg->desc );
+
+      if( reg->type == k_vg_opt_type_long_flag )
+         printf( "--%-21s %s\n", reg->alias, reg->desc );
+
+      if( reg->type == k_vg_opt_type_long_arg )
+      {
+         char temp[32];
+         snprintf( temp, 32, "--%s=<value>", reg->alias );
+         printf( "%-23s %s\n", temp, reg->desc );
+      }
+
+      if( reg->type == k_vg_opt_type_standard )
+      {
+         printf( "<standard>                %s\n", reg->desc );
+      }
+   }
+}
+
 /* Will return 0 if exhausted */
 int vg_argp( int argc, char *argv[] )
 {
@@ -70,8 +153,10 @@ int vg_argp( int argc, char *argv[] )
 }
 
 /* Example: see if -c is set */
-int vg_opt( char c )
+int vg_opt( char c, const char *desc )
 {
+   _vg_opt_observe( NULL, c, desc, k_vg_opt_type_flag );
+
    char *carg = vg_argv[ vg_argi ];
 
    if( carg[0] == '-' )
@@ -91,9 +176,11 @@ int vg_opt( char c )
 }
 
 /* Example: get -c *value* */
-char *vg_opt_arg( char c )
+char *vg_opt_arg( char c, const char *desc )
 {
-   if( vg_opt( c ) )
+   _vg_opt_observe( NULL, c, desc, k_vg_opt_type_arg );
+
+   if( vg_opt( c, NULL ) )
    {
       if( vg_argi < vg_argc-1 )
       {
@@ -112,8 +199,10 @@ char *vg_opt_arg( char c )
 }
 
 /* Example see if --big is set */
-int vg_long_opt( char *name )
+int vg_long_opt( char *name, const char *desc )
 {
+   _vg_opt_observe( name, 0, desc, k_vg_opt_type_long_flag );
+
    char *carg = vg_argv[ vg_argi ];
    
    if( carg[0] == '-' )
@@ -132,8 +221,10 @@ int vg_long_opt( char *name )
 }
 
 /* Example: get --big=value */
-char *vg_long_opt_arg( char *name )
+char *vg_long_opt_arg( char *name, const char *desc )
 {
+   _vg_opt_observe( name, 0, desc, k_vg_opt_type_long_arg );
+
    char *carg = vg_argv[ vg_argi ];
    
    if( carg[0] == '-' )
@@ -173,8 +264,10 @@ char *vg_long_opt_arg( char *name )
 }
 
 /* Example: get regular_thing */
-char *vg_arg(void)
+char *vg_arg( const char *desc )
 {
+   _vg_opt_observe( NULL, 0, desc, k_vg_opt_type_standard );
+
    char *carg = vg_argv[ vg_argi ];
    
    if( carg[0] != '-' )
index 6f2d8792f8acd230230fc3d8befc8ce4921dcec3..0dac45ce26a771433c67eeb041e5456a47ce7a85 100644 (file)
--- a/vg_opt.h
+++ b/vg_opt.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ * Copyright (C) 2021-2024 Mt.ZERO Software - All Rights Reserved
  */
 
 #pragma once
@@ -8,16 +8,19 @@
 int vg_argp( int argc, char *argv[] );
 
 /* Example: see if -c is set */
-int vg_opt( char c );
+int vg_opt( char c, const char *desc );
 
 /* Example: get -c *value* */
-char *vg_opt_arg( char c );
+char *vg_opt_arg( char c, const char *desc );
 
 /* Example see if --big is set */
-int vg_long_opt( char *name );
+int vg_long_opt( char *name, const char *desc );
 
 /* Example: get --big=value */
-char *vg_long_opt_arg( char *name );
+char *vg_long_opt_arg( char *name, const char *desc );
 
 /* Example: get regular_thing */
-char *vg_arg(void);
+char *vg_arg( const char *desc );
+
+/* You should probably bind this to --help (and call it last!) */
+void _vg_print_options(void);