Improvements to the command-line argument parsing in the
command-line shell. Command-line options can now occur either before or after the database name and first command and are still accepted and processed. Command-line options are processed even if no database name is given (and :memory: is assumed). FossilOrigin-Name: 317c80cba3688a97ade9cde622cc3bd94cf3436a
This commit is contained in:
parent
8749c1831a
commit
98d312fc3e
14
manifest
14
manifest
@ -1,5 +1,5 @@
|
||||
C Make\ssure\ssubstructure\selements\shave\sproper\salignment\sin\sthe\sICU\stokenizers\nof\sFTS2\sand\sFTS3.
|
||||
D 2012-10-19T02:10:53.511
|
||||
C Improvements\sto\sthe\scommand-line\sargument\sparsing\sin\sthe\s\ncommand-line\sshell.\s\sCommand-line\soptions\scan\snow\soccur\seither\sbefore\nor\safter\sthe\sdatabase\sname\sand\sfirst\scommand\sand\sare\sstill\saccepted\sand\nprocessed.\s\sCommand-line\soptions\sare\sprocessed\seven\sif\sno\sdatabase\sname\nis\sgiven\s(and\s:memory:\sis\sassumed).
|
||||
D 2012-10-25T15:23:14.369
|
||||
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
|
||||
F Makefile.in 5f4f26109f9d80829122e0e09f9cda008fa065fb
|
||||
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
|
||||
@ -175,7 +175,7 @@ F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
|
||||
F src/resolve.c 7b986a715ac281643309c29257bb58cfae7aa810
|
||||
F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
|
||||
F src/select.c 9b759521f805e25e97baa2dd64037157fe365817
|
||||
F src/shell.c 8ee5a3cb502e2d574f97b43972e6c1e275e7bec7
|
||||
F src/shell.c d570e6cb3889eb07fdf15808a1b6b136c78e4da5
|
||||
F src/sqlite.h.in c7be05ad191d2634292fcc77bdb2bcfa4526eb98
|
||||
F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0
|
||||
F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
|
||||
@ -706,7 +706,7 @@ F test/shared8.test b27befbefbe7f4517f1d6b7ff8f64a41ec74165d
|
||||
F test/shared9.test 614a3ca431adc73c857632deb4eff75bcaee40ec
|
||||
F test/shared_err.test 91e26ec4f3fbe07951967955585137e2f18993de
|
||||
F test/sharedlock.test ffa0a3c4ac192145b310f1254f8afca4d553eabf
|
||||
F test/shell1.test 272384163432c0efd2c6817396beb0d119565d53
|
||||
F test/shell1.test 340125225cc96c7f48d59a869aaf82866e481007
|
||||
F test/shell2.test 037d6ad16e873354195d30bb2dc4b5321788154a
|
||||
F test/shell3.test 9196c42772d575685e722c92b4b39053c6ebba59
|
||||
F test/shell4.test aa4eef8118b412d1a01477a53426ece169ea86a9
|
||||
@ -1021,7 +1021,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
|
||||
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
|
||||
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
|
||||
F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9
|
||||
P 0482c73bfdf80b0c0ce9abea706554d7ddf36f69
|
||||
R 359b18ae3c286a09e1c7579425de96fe
|
||||
P aaa2d9b0db74d8452d9294de17cff786ab4ec7c8
|
||||
R 4204a6c0a11b862c569be1046625095c
|
||||
U drh
|
||||
Z 09f8af841cfaabcfb0c92e8b07cb012e
|
||||
Z 98d27aa00ef0fe7caf71f35a32885811
|
||||
|
@ -1 +1 @@
|
||||
aaa2d9b0db74d8452d9294de17cff786ab4ec7c8
|
||||
317c80cba3688a97ade9cde622cc3bd94cf3436a
|
101
src/shell.c
101
src/shell.c
@ -2824,11 +2824,14 @@ static const char zOptions[] =
|
||||
" -bail stop after hitting an error\n"
|
||||
" -batch force batch I/O\n"
|
||||
" -column set output mode to 'column'\n"
|
||||
" -cmd command run \"command\" before reading stdin\n"
|
||||
" -cmd COMMIT run \"COMMAND\" before reading stdin\n"
|
||||
" -csv set output mode to 'csv'\n"
|
||||
" -echo print commands before execution\n"
|
||||
" -init filename read/process named file\n"
|
||||
" -[no]header turn headers on or off\n"
|
||||
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
|
||||
" -heap SIZE Size of heap for memsys3 or memsys5\n"
|
||||
#endif
|
||||
" -help show this message\n"
|
||||
" -html set output mode to HTML\n"
|
||||
" -interactive force interactive I/O\n"
|
||||
@ -2837,8 +2840,8 @@ static const char zOptions[] =
|
||||
#ifdef SQLITE_ENABLE_MULTIPLEX
|
||||
" -multiplex enable the multiplexor VFS\n"
|
||||
#endif
|
||||
" -nullvalue 'text' set text string for NULL values\n"
|
||||
" -separator 'x' set output field separator (|)\n"
|
||||
" -nullvalue TEXT set text string for NULL values. Default ''\n"
|
||||
" -separator SEP set output field separator. Default: '|'\n"
|
||||
" -stats print memory stats before each finalize\n"
|
||||
" -version show SQLite version\n"
|
||||
" -vfs NAME use NAME as the default VFS\n"
|
||||
@ -2874,6 +2877,19 @@ static void main_init(struct callback_data *data) {
|
||||
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
|
||||
}
|
||||
|
||||
/*
|
||||
** Get the argument to an --option. Throw an error and die if no argument
|
||||
** is available.
|
||||
*/
|
||||
static char *cmdline_option_value(int argc, char **argv, int i){
|
||||
if( i==argc ){
|
||||
fprintf(stderr, "%s: Error: missing argument to %s\n",
|
||||
argv[0], argv[argc-1]);
|
||||
exit(1);
|
||||
}
|
||||
return argv[i];
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
char *zErrMsg = 0;
|
||||
struct callback_data data;
|
||||
@ -2903,24 +2919,35 @@ int main(int argc, char **argv){
|
||||
** the size of the alternative malloc heap,
|
||||
** and the first command to execute.
|
||||
*/
|
||||
for(i=1; i<argc-1; i++){
|
||||
for(i=1; i<argc; i++){
|
||||
char *z;
|
||||
if( argv[i][0]!='-' ) break;
|
||||
z = argv[i];
|
||||
if( z[0]!='-' ){
|
||||
if( data.zDbFilename==0 ){
|
||||
data.zDbFilename = z;
|
||||
continue;
|
||||
}
|
||||
if( zFirstCmd==0 ){
|
||||
zFirstCmd = z;
|
||||
continue;
|
||||
}
|
||||
fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
|
||||
fprintf(stderr,"Use -help for a list of options.\n");
|
||||
return 1;
|
||||
}
|
||||
if( z[1]=='-' ) z++;
|
||||
if( strcmp(z,"-separator")==0
|
||||
|| strcmp(z,"-nullvalue")==0
|
||||
|| strcmp(z,"-cmd")==0
|
||||
){
|
||||
i++;
|
||||
(void)cmdline_option_value(argc, argv, ++i);
|
||||
}else if( strcmp(z,"-init")==0 ){
|
||||
i++;
|
||||
zInitFile = argv[i];
|
||||
/* Need to check for batch mode here to so we can avoid printing
|
||||
** informational messages (like from process_sqliterc) before
|
||||
** we do the actual processing of arguments later in a second pass.
|
||||
*/
|
||||
zInitFile = cmdline_option_value(argc, argv, ++i);
|
||||
}else if( strcmp(z,"-batch")==0 ){
|
||||
/* Need to check for batch mode here to so we can avoid printing
|
||||
** informational messages (like from process_sqliterc) before
|
||||
** we do the actual processing of arguments later in a second pass.
|
||||
*/
|
||||
stdin_is_interactive = 0;
|
||||
}else if( strcmp(z,"-heap")==0 ){
|
||||
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
|
||||
@ -2928,7 +2955,7 @@ int main(int argc, char **argv){
|
||||
const char *zSize;
|
||||
sqlite3_int64 szHeap;
|
||||
|
||||
zSize = argv[++i];
|
||||
zSize = cmdline_option_value(argc, argv, ++i);
|
||||
szHeap = atoi(zSize);
|
||||
for(j=0; (c = zSize[j])!=0; j++){
|
||||
if( c=='M' ){ szHeap *= 1000000; break; }
|
||||
@ -2955,7 +2982,7 @@ int main(int argc, char **argv){
|
||||
sqlite3_multiplex_initialize(0, 1);
|
||||
#endif
|
||||
}else if( strcmp(z,"-vfs")==0 ){
|
||||
sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
|
||||
sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
|
||||
if( pVfs ){
|
||||
sqlite3_vfs_register(pVfs, 1);
|
||||
}else{
|
||||
@ -2964,31 +2991,15 @@ int main(int argc, char **argv){
|
||||
}
|
||||
}
|
||||
}
|
||||
if( i<argc ){
|
||||
data.zDbFilename = argv[i++];
|
||||
}else{
|
||||
if( data.zDbFilename==0 ){
|
||||
#ifndef SQLITE_OMIT_MEMORYDB
|
||||
data.zDbFilename = ":memory:";
|
||||
#else
|
||||
data.zDbFilename = 0;
|
||||
#endif
|
||||
}
|
||||
if( i<argc ){
|
||||
zFirstCmd = argv[i++];
|
||||
}
|
||||
if( i<argc ){
|
||||
fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
|
||||
fprintf(stderr,"Use -help for a list of options.\n");
|
||||
return 1;
|
||||
}
|
||||
data.out = stdout;
|
||||
|
||||
#ifdef SQLITE_OMIT_MEMORYDB
|
||||
if( data.zDbFilename==0 ){
|
||||
fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
data.out = stdout;
|
||||
|
||||
/* Go ahead and open the database file if it already exists. If the
|
||||
** file does not exist, delay opening it. This prevents empty database
|
||||
@ -3013,8 +3024,9 @@ int main(int argc, char **argv){
|
||||
** file is processed so that the command-line arguments will override
|
||||
** settings in the initialization file.
|
||||
*/
|
||||
for(i=1; i<argc && argv[i][0]=='-'; i++){
|
||||
for(i=1; i<argc; i++){
|
||||
char *z = argv[i];
|
||||
if( z[0]!='-' ) continue;
|
||||
if( z[1]=='-' ){ z++; }
|
||||
if( strcmp(z,"-init")==0 ){
|
||||
i++;
|
||||
@ -3030,25 +3042,11 @@ int main(int argc, char **argv){
|
||||
data.mode = MODE_Csv;
|
||||
memcpy(data.separator,",",2);
|
||||
}else if( strcmp(z,"-separator")==0 ){
|
||||
i++;
|
||||
if(i>=argc){
|
||||
fprintf(stderr,"%s: Error: missing argument for option: %s\n",
|
||||
Argv0, z);
|
||||
fprintf(stderr,"Use -help for a list of options.\n");
|
||||
return 1;
|
||||
}
|
||||
sqlite3_snprintf(sizeof(data.separator), data.separator,
|
||||
"%.*s",(int)sizeof(data.separator)-1,argv[i]);
|
||||
"%s",cmdline_option_value(argc,argv,++i));
|
||||
}else if( strcmp(z,"-nullvalue")==0 ){
|
||||
i++;
|
||||
if(i>=argc){
|
||||
fprintf(stderr,"%s: Error: missing argument for option: %s\n",
|
||||
Argv0, z);
|
||||
fprintf(stderr,"Use -help for a list of options.\n");
|
||||
return 1;
|
||||
}
|
||||
sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
|
||||
"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
|
||||
"%s",cmdline_option_value(argc,argv,++i));
|
||||
}else if( strcmp(z,"-header")==0 ){
|
||||
data.showHeader = 1;
|
||||
}else if( strcmp(z,"-noheader")==0 ){
|
||||
@ -3082,8 +3080,7 @@ int main(int argc, char **argv){
|
||||
usage(1);
|
||||
}else if( strcmp(z,"-cmd")==0 ){
|
||||
if( i==argc-1 ) break;
|
||||
i++;
|
||||
z = argv[i];
|
||||
z = cmdline_option_value(argc,argv,++i);
|
||||
if( z[0]=='.' ){
|
||||
rc = do_meta_command(z, &data);
|
||||
if( rc && bail_on_error ) return rc;
|
||||
|
@ -149,7 +149,7 @@ do_test shell1-1.14.3 {
|
||||
set res [catchcmd "-separator" ""]
|
||||
set rc [lindex $res 0]
|
||||
list $rc \
|
||||
[regexp {Error: missing argument for option: -separator} $res]
|
||||
[regexp {Error: missing argument to -separator} $res]
|
||||
} {1 1}
|
||||
|
||||
# -stats print memory stats before each finalize
|
||||
@ -168,7 +168,7 @@ do_test shell1-1.15.3 {
|
||||
set res [catchcmd "-nullvalue" ""]
|
||||
set rc [lindex $res 0]
|
||||
list $rc \
|
||||
[regexp {Error: missing argument for option: -nullvalue} $res]
|
||||
[regexp {Error: missing argument to -nullvalue} $res]
|
||||
} {1 1}
|
||||
|
||||
# -version show SQLite version
|
||||
|
Loading…
x
Reference in New Issue
Block a user