Skip to content

Commit a34ef0a

Browse files
committed
Refactor command-line argument parsing to use argparse library with improved help text and examples
1 parent bf42008 commit a34ef0a

1 file changed

Lines changed: 95 additions & 44 deletions

File tree

vark.cpp

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#define VARK_IMPLEMENTATION
2929
#include "vark.h"
30+
#include "argparse/argparse.hpp"
3031

3132
namespace fs = std::filesystem;
3233

@@ -60,60 +61,108 @@ static void ProcessFiles( Vark& vark, const std::vector<std::string>& files, con
6061
}
6162
}
6263

63-
void PrintUsage()
64-
{
65-
printf( "Usage:\n" );
66-
printf( " vark -c <archive> <files...>\tCreate archive\n" );
67-
printf( " vark -cs <archive> <files...>\tCreate archive (sharded compression)\n" );
68-
printf( " vark -a <archive> <files...>\tAppend to archive\n" );
69-
printf( " vark -as <archive> <files...>\tAppend to archive (sharded)\n" );
70-
printf( " vark -x <archive>\t\tExtract archive\n" );
71-
printf( " vark -l <archive>\t\tList archive contents\n" );
72-
printf( " vark -v <archive>\t\tVerify archive integrity\n" );
73-
printf( " vark <archive>\t\tSmart mode (Extract if .vark, Create/Append otherwise)\n" );
74-
}
75-
7664
#ifdef VARK_UNIT_TEST_MODE
7765
int vark_test_main( int argc, char** argv )
7866
#else // !VARK_UNIT_TEST_MODE
7967
int main( int argc, char** argv )
8068
#endif // VARK_UNIT_TEST_MODE
8169
{
82-
std::string mode = "";
83-
std::string archivePath;
84-
std::string arg1;
85-
std::vector<std::string> inputFiles;
86-
Vark vark;
87-
int argIdx = 1;
88-
bool exists = false;
89-
bool hasInputs = false;
70+
argparse::ArgumentParser program( "vark", "1.04" );
71+
72+
program.add_description( "A minimal LZAV archive tool for fast compression and decompression. Supports sharded compression for efficient random access to large files. Use without flags for smart mode: extracts existing archives or creates new ones automatically." );
73+
74+
program.add_argument( "-c" )
75+
.help( "Create archive" )
76+
.default_value( false )
77+
.implicit_value( true );
78+
79+
program.add_argument( "-cs" )
80+
.help( "Create archive (sharded compression)" )
81+
.default_value( false )
82+
.implicit_value( true );
9083

91-
if ( argc < 2 )
84+
program.add_argument( "-a" )
85+
.help( "Append to archive" )
86+
.default_value( false )
87+
.implicit_value( true );
88+
89+
program.add_argument( "-as" )
90+
.help( "Append to archive (sharded)" )
91+
.default_value( false )
92+
.implicit_value( true );
93+
94+
program.add_argument( "-x" )
95+
.help( "Extract archive" )
96+
.default_value( false )
97+
.implicit_value( true );
98+
99+
program.add_argument( "-l" )
100+
.help( "List archive contents" )
101+
.default_value( false )
102+
.implicit_value( true );
103+
104+
program.add_argument( "-v" )
105+
.help( "Verify archive integrity" )
106+
.default_value( false )
107+
.implicit_value( true );
108+
109+
program.add_argument( "archive" )
110+
.help( "archive file path" );
111+
112+
program.add_argument( "files" )
113+
.remaining()
114+
.help( "input files or directories" );
115+
116+
program.add_epilog( "Examples:\n"
117+
" vark data.vark Extract archive (smart mode)\n"
118+
" vark -c game.vark assets/ Create archive from directory\n"
119+
" vark -cs textures.vark images/ Create with sharded compression\n"
120+
" vark -a game.vark newfile.dat Append file to existing archive\n"
121+
" vark -l game.vark List archive contents" );
122+
123+
try
124+
{
125+
program.parse_args( argc, argv );
126+
}
127+
catch ( const std::runtime_error& err )
92128
{
93-
PrintUsage();
129+
if ( argc > 1 ) std::cerr << "Error: " << err.what() << std::endl;
130+
std::cerr << program;
94131
return 1;
95132
}
96133

97-
arg1 = argv[argIdx];
98-
99-
if ( arg1 == "-c" || arg1 == "-cs" || arg1 == "-x" || arg1 == "-a" || arg1 == "-as" || arg1 == "-l" || arg1 == "-v" )
134+
std::string archivePath = program.get<std::string>( "archive" );
135+
std::vector<std::string> rawInputFiles;
136+
try
100137
{
101-
mode = arg1;
102-
argIdx++;
103-
if ( argIdx >= argc )
104-
{
105-
printf( "Error: Missing archive name.\n" );
106-
return 1;
107-
}
108-
archivePath = argv[argIdx++];
138+
rawInputFiles = program.get<std::vector<std::string>>( "files" );
109139
}
110-
else
140+
catch ( ... )
141+
{
142+
// No files provided
143+
}
144+
145+
std::string mode = "";
146+
int flagsSet = 0;
147+
if ( program["-c"] == true ) { mode = "-c"; flagsSet++; }
148+
if ( program["-cs"] == true ) { mode = "-cs"; flagsSet++; }
149+
if ( program["-a"] == true ) { mode = "-a"; flagsSet++; }
150+
if ( program["-as"] == true ) { mode = "-as"; flagsSet++; }
151+
if ( program["-x"] == true ) { mode = "-x"; flagsSet++; }
152+
if ( program["-l"] == true ) { mode = "-l"; flagsSet++; }
153+
if ( program["-v"] == true ) { mode = "-v"; flagsSet++; }
154+
155+
if ( flagsSet > 1 )
111156
{
112-
archivePath = arg1;
113-
argIdx++;
157+
std::cerr << "Error: Multiple mode flags set." << std::endl;
158+
return 1;
159+
}
114160

115-
exists = fs::exists( archivePath );
116-
hasInputs = ( argIdx < argc );
161+
if ( flagsSet == 0 )
162+
{
163+
// Smart mode
164+
bool exists = fs::exists( archivePath );
165+
bool hasInputs = !rawInputFiles.empty();
117166

118167
if ( exists && !hasInputs )
119168
{
@@ -123,20 +172,22 @@ int main( int argc, char** argv )
123172
{
124173
mode = "-a";
125174
}
126-
else {
175+
else
176+
{
127177
mode = "-c";
128178
}
129179
}
130180

131-
// Gather input files
132-
for ( ; argIdx < argc; ++argIdx )
181+
std::vector<std::string> inputFiles;
182+
for ( const auto& path : rawInputFiles )
133183
{
134-
AddToFiles( inputFiles, argv[argIdx] );
184+
AddToFiles( inputFiles, path );
135185
}
136186

137187
uint32_t compressFlags = 0;
138188
if ( mode == "-cs" || mode == "-as" ) compressFlags |= VARK_COMPRESS_SHARDED;
139189

190+
Vark vark;
140191
if ( mode == "-c" || mode == "-cs" )
141192
{
142193
if ( inputFiles.empty() )
@@ -300,7 +351,7 @@ int main( int argc, char** argv )
300351
}
301352
else
302353
{
303-
PrintUsage();
354+
std::cerr << program;
304355
return 1;
305356
}
306357

0 commit comments

Comments
 (0)