Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Reworking how fcli flag handling is done. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | fcli-flag-rework |
Files: | files | file ages | folders |
SHA1: |
dad2f76f30856b525f2731777cbedeee |
User & Date: | stephan 2021-02-26 14:04:32.095 |
Context
2021-02-26
| ||
16:47 | More work on the fcli argument handling rewrite. check-in: 490b7ab32e user: stephan tags: fcli-flag-rework | |
14:04 | Reworking how fcli flag handling is done. check-in: dad2f76f30 user: stephan tags: fcli-flag-rework | |
13:10 | Removed leftover VC6 projects files. check-in: 6585894b76 user: stephan tags: trunk | |
Changes
Changes to f-apps/f-acat.c.
︙ | ︙ | |||
33 34 35 36 37 38 39 | "Default is stdout.\n"); puts("\n\t--raw fetches blobs in raw form, which means that no " "undeltification is applied (but they are decompressed, if " "needed).\n"); } | | | 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | "Default is stdout.\n"); puts("\n\t--raw fetches blobs in raw form, which means that no " "undeltification is applied (but they are decompressed, if " "needed).\n"); } int main(int argc, char const * const * argv ){ int rc = 0; const char * sym = NULL; const char * ofileName = NULL; FILE * ofile = NULL; fsl_cx * f; fsl_db * db; fsl_id_t rid = 0; |
︙ | ︙ |
Changes to f-apps/f-add.c.
︙ | ︙ | |||
27 28 29 30 31 32 33 | printf("Usage:\n\t%s [options] file [...files]\n\n", fcli.appName); puts("Queues up files to be added at the next commit. Options:\n"); puts("\t--dry-run|-n runs the operation in a transaction and then rolls it back.\n"); } | | | 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | printf("Usage:\n\t%s [options] file [...files]\n\n", fcli.appName); puts("Queues up files to be added at the next commit. Options:\n"); puts("\t--dry-run|-n runs the operation in a transaction and then rolls it back.\n"); } int main(int argc, char const * const * argv ){ int rc = 0; const char * fname; fsl_cx * f; char inTrans = 0; fsl_db * db; char dryRun; fsl_id_t ckoutId = 0; |
︙ | ︙ |
Changes to f-apps/f-adiff.c.
︙ | ︙ | |||
64 65 66 67 68 69 70 | opt->contextLines, opt->sbsWidth, opt->diffFlags); fsl_buffer_clear(&lhs); fsl_buffer_clear(&rhs); return rc; } | | | 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | opt->contextLines, opt->sbsWidth, opt->diffFlags); fsl_buffer_clear(&lhs); fsl_buffer_clear(&rhs); return rc; } int main(int argc, char const * const * argv ){ int rc = 0; const char * vFrom = NULL; const char * vTo = NULL; const char * tmpVal = NULL; fsl_cx * f; fsl_id_t idFrom = 0, idTo = 0; ADiffOpt diffOpt = { |
︙ | ︙ |
Changes to f-apps/f-checkin.c.
︙ | ︙ | |||
89 90 91 92 93 94 95 | Reminder: name of a file by its content RID: SELECT fn.name FROM filename fn, mlink ml WHERE fn.fnid=ml.fnid AND ml.fid=$contentRid */ | | | 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | Reminder: name of a file by its content RID: SELECT fn.name FROM filename fn, mlink ml WHERE fn.fnid=ml.fnid AND ml.fid=$contentRid */ int main(int argc, char const * const * argv ){ int rc = 0; fsl_cx * f; char inTrans = 0; fsl_db * db; char dryRun = 0; fsl_id_t ckoutId = 0; const char * cMsg = NULL; |
︙ | ︙ |
Changes to f-apps/f-config.c.
︙ | ︙ | |||
193 194 195 196 197 198 199 | return fcli_err_set(FSL_RC_MISUSE, "No config db specified. Use one of: " "-g[lobal], -r[epo], -c[heckout]"); } return fcli_dispatch_commands(ConfigCmds, 0); } | | | 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | return fcli_err_set(FSL_RC_MISUSE, "No config db specified. Use one of: " "-g[lobal], -r[epo], -c[heckout]"); } return fcli_dispatch_commands(ConfigCmds, 0); } int main(int argc, char const * const * argv ){ int rc = 0; fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); if(FSL_RC_BREAK==rc) /* --help */ return 0; else if(rc) goto end; rc = fapp_main(fcli_cx()); end: |
︙ | ︙ |
Changes to f-apps/f-delta.c.
︙ | ︙ | |||
32 33 34 35 36 37 38 | puts("The first form outputs a fossil-format " "delta of two files."); puts("The second form applies a delta (the first file) " "to the second file."); puts("\nAll output goes to stdout.\n"); } | | | 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | puts("The first form outputs a fossil-format " "delta of two files."); puts("The second form applies a delta (the first file) " "to the second file."); puts("\nAll output goes to stdout.\n"); } int main(int argc, char const * const * argv ){ int rc = 0; const char * f1 = NULL; const char * f2 = NULL; char doApply = 0; fsl_error err = fsl_error_empty; fsl_buffer b1 = fsl_buffer_empty, b2 = fsl_buffer_empty, |
︙ | ︙ |
Changes to f-apps/f-event.c.
︙ | ︙ | |||
134 135 136 137 138 139 140 | fsl_buffer_clear(&dout); fsl_buffer_clear(&hash); fsl_deck_finalize(d); return rc; } | | | 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | fsl_buffer_clear(&dout); fsl_buffer_clear(&hash); fsl_deck_finalize(d); return rc; } int main(int argc, char const * const * argv ){ int rc = 0; fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); if(FSL_RC_BREAK==rc) return 0; else if(rc) goto end; App.crossLink = fcli_flag2("c", "crosslink", 0); |
︙ | ︙ |
Changes to f-apps/f-ls.c.
︙ | ︙ | |||
86 87 88 89 90 91 92 | fsl_uuid_to_rid(fcli.f, fc->uuid)); } f_out("%.*s %c %s\n", 12, fc->uuid, perm, fc->name); } return 0; } | | | 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | fsl_uuid_to_rid(fcli.f, fc->uuid)); } f_out("%.*s %c %s\n", 12, fc->uuid, perm, fc->name); } return 0; } int main(int argc, char const * const * argv ){ int rc = 0; const char * lsVersion = NULL; fsl_cx * f; fsl_deck deck = fsl_deck_empty; fsl_deck * d = &deck; char onlyFromSelf = 0; |
︙ | ︙ |
Changes to f-apps/f-mfparse.c.
︙ | ︙ | |||
25 26 27 28 29 30 31 | #include <time.h> #ifndef _WIN32 #include <unistd.h> /*isatty()*/ #endif static struct App_{ | | | > | | > | 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <time.h> #ifndef _WIN32 #include <unistd.h> /*isatty()*/ #endif static struct App_{ bool doCrosslink; bool checkRCard; char const * mfile; } App = { false/*doCrosslink*/, false/*checkRCard*/, 0 }; /* Just for testing default crosslinker replacement */ #define MY_OVERRIDE_XLINK_CHECKIN 0 /** Just experimenting with fsl_xlink_listener() and friends. |
︙ | ︙ | |||
237 238 239 240 241 242 243 | fsl_deck_finalize(&mf); return rc; } static void fcli_local_help(){ | < | < < < < < < < | | > > | < | > > > > > > > > > > > > > > > > | < < < < < < < < < < < < < < < < < < < < > > > > > > > > > > > > > > > > | 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | fsl_deck_finalize(&mf); return rc; } static void fcli_local_help(){ puts("If -f is not used, it reads from the first non-flag argument."); puts("On non-Windows sytems (those with isatty(3)) " "it will read from stdin if stdin is not a terminal and " "neither -f nor any non-flag arguments are provided."); } int main(int argc, char const * const * argv ){ int rc = 0; fsl_cx * f; fsl_db * db; bool failCrosslink = 0; const char * mfile = 0; fcli.appHelp = fcli_local_help; fcli_flag_entry FCliFlags[] = { FCLI_FLAG_BOOL("c","crosslink",&App.doCrosslink, "Crosslink the parsed artifact."), FCLI_FLAG("f","file",&mfile, "Read artifact from this file. " "Default is the first non-flag argument."), FCLI_FLAG_BOOL("r","r-card",&App.checkRCard, "Confirms the R-card value (if any) on the parsed parsed artifact."), FCLI_FLAG_BOOL("fx","fail-xlink",&failCrosslink, "Causes crosslinking (if enabled) to forcibly fail."), fcli_flag_entry_empty_m }; rc = fcli_setup2(argc, argv, FCliFlags); if(FSL_RC_BREAK==rc) /* --help */ return 0; else if(rc) goto end; if(!mfile){ mfile = fcli_next_arg(1); #ifndef _WIN32 if(!mfile && !isatty(0)){ mfile = "-"; }else #endif if(!mfile){ fcli_help(); rc = FSL_RC_MISUSE; goto end; } } f = fcli.f; db = fsl_cx_db_repo(f); if(!db){ rc = fsl_cx_err_set(f, FSL_RC_MISUSE, "This app requires a repository db."); goto end; } fsl_xlink_listener( f, #if !MY_OVERRIDE_XLINK_CHECKIN fcli.appName, #else "fsl/checkin/timeline", #endif my_xlink_f, &failCrosslink ); if(fcli_has_unused_flags(0)) goto end; rc = test_parse_1(mfile); end: return (fcli_err_report(1) || rc) ? EXIT_FAILURE : EXIT_SUCCESS; } |
Changes to f-apps/f-new.c.
︙ | ︙ | |||
145 146 147 148 149 150 151 | P("auto",FSL_HPOLICY_SHA3/*yes, SHA3*/); P("sha1",FSL_HPOLICY_SHA1); P("shun-sha1",FSL_HPOLICY_SHA3); #undef P return fcli_err_set(FSL_RC_MISUSE,"Invalid --hash policy value."); } | | | 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | P("auto",FSL_HPOLICY_SHA3/*yes, SHA3*/); P("sha1",FSL_HPOLICY_SHA1); P("shun-sha1",FSL_HPOLICY_SHA3); #undef P return fcli_err_set(FSL_RC_MISUSE,"Invalid --hash policy value."); } int main(int argc, char const * const * argv ){ int rc = 0; fsl_cx * f; char force; fcli.checkoutDir = NULL /* Same effect as -C/--no-checkout */; fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); if(FSL_RC_BREAK==rc) /* --help */ return 0; |
︙ | ︙ |
Changes to f-apps/f-parseparty.c.
︙ | ︙ | |||
222 223 224 225 226 227 228 | "t=ticket, n=technote, f=forum"); puts("\t-s|--skip-unknown when crosslinking, skip over type(s) we " "don't know how to crosslink (tickets).\n"); puts("\t e.g. --types=cgw"); } | | | 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | "t=ticket, n=technote, f=forum"); puts("\t-s|--skip-unknown when crosslinking, skip over type(s) we " "don't know how to crosslink (tickets).\n"); puts("\t e.g. --types=cgw"); } int main(int argc, char const * const * argv ){ int rc = 0; fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); if(FSL_RC_BREAK==rc) /* --help */ return EXIT_SUCCESS; else if(rc) goto end; |
︙ | ︙ |
Changes to f-apps/f-query.c.
︙ | ︙ | |||
88 89 90 91 92 93 94 | char const * col = fsl_stmt_g_text(stmt, i, NULL); f_out("%s%s", col ? col : "NULL", (i<(stmt->colCount-1)) ? sep : "\n"); } return 0; } | | | 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | char const * col = fsl_stmt_g_text(stmt, i, NULL); f_out("%s%s", col ? col : "NULL", (i<(stmt->colCount-1)) ? sep : "\n"); } return 0; } int main(int argc, char const * const * argv ){ int rc = 0; const char * sql = NULL; fsl_cx * f; fsl_db * db; char qCount = 0; fsl_buffer scriptBuf = fsl_buffer_empty; char noTransaction = 0; |
︙ | ︙ |
Changes to f-apps/f-resolve.c.
︙ | ︙ | |||
32 33 34 35 36 37 38 | puts("\t-sb|--start-of-branch: expects the symbolic name to be a branch name " "and resolves to the first checkin in that branch, rather than the " "more recent. Results are unspecified if the branch name is used by " "multiple branches."); puts(""); } | | | 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | puts("\t-sb|--start-of-branch: expects the symbolic name to be a branch name " "and resolves to the first checkin in that branch, rather than the " "more recent. Results are unspecified if the branch name is used by " "multiple branches."); puts(""); } int main(int argc, char const * const * argv ){ int rc = 0; const char * sym = NULL; fsl_cx * f; fsl_db * db; int count = 0; fsl_id_t rid; fsl_uuid_str uuid; |
︙ | ︙ |
Changes to f-apps/f-rm.c.
︙ | ︙ | |||
26 27 28 29 30 31 32 | printf("Usage:\n\t%s [options] file [...files]\n\n", fcli.appName); puts("Queues up files to be removed at the next commit. Options:\n"); puts("\t--dry-run|-n runs the operation in a transaction and then rolls it back.\n"); } | | | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | printf("Usage:\n\t%s [options] file [...files]\n\n", fcli.appName); puts("Queues up files to be removed at the next commit. Options:\n"); puts("\t--dry-run|-n runs the operation in a transaction and then rolls it back.\n"); } int main(int argc, char const * const * argv ){ int rc = 0; const char * fname; fsl_cx * f; char inTrans = 0; fsl_db * db; char dryRun; fcli.appHelp = fapp_local_help; |
︙ | ︙ |
Changes to f-apps/f-sanity.c.
︙ | ︙ | |||
1782 1783 1784 1785 1786 1787 1788 | } #endif /* FSLPRINTF_ENABLE_JSON */ /* static */ void test_repo_is_readonly(){ /* TODO */ } | | | 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 | } #endif /* FSLPRINTF_ENABLE_JSON */ /* static */ void test_repo_is_readonly(){ /* TODO */ } int main(int argc, char const * const * argv ){ int rc = 0; fsl_cx * f; char singleTest; fsl_timer_state timer = fsl_timer_state_empty; fsl_timer_start(&timer); fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); |
︙ | ︙ |
Changes to f-apps/f-status.c.
︙ | ︙ | |||
212 213 214 215 216 217 218 | fsl_stmt_finalize(&st); return rc; } | | | 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | fsl_stmt_finalize(&st); return rc; } int main(int argc, char const * const * argv ){ int rc = 0; fsl_cx * f; fsl_db * db; fsl_id_t rid = 0; char skipFiles; char useUtc; const char * zVersion = NULL; |
︙ | ︙ |
Changes to f-apps/f-tag.c.
︙ | ︙ | |||
62 63 64 65 66 67 68 | fsl_satype_cstr(d->type), 8, d->uuid, d->rid)); return *((char const *)state) /* demonstrate what happens when crosslinking fails. */ ? FSL_RC_IO : 0; } | | | 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | fsl_satype_cstr(d->type), 8, d->uuid, d->rid)); return *((char const *)state) /* demonstrate what happens when crosslinking fails. */ ? FSL_RC_IO : 0; } int main(int argc, char const * const * argv ){ int rc = 0; const char * symToTag = NULL; char dryRun = 0; fsl_cx * f = 0; fsl_db * db = 0; char failCrosslink = 0; char const * userName = 0; |
︙ | ︙ |
Changes to f-apps/f-timeline.c.
︙ | ︙ | |||
250 251 252 253 254 255 256 | puts("\t--tag|-t=TAG Filters on the given tag name. " "--tag=sym-XYZ is equivalent to --branch=XYZ.\n"); puts("\t--utc Changes time values from local time to UTC.\n"); } | | | 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | puts("\t--tag|-t=TAG Filters on the given tag name. " "--tag=sym-XYZ is equivalent to --branch=XYZ.\n"); puts("\t--utc Changes time values from local time to UTC.\n"); } int main(int argc, char const * const * argv ){ int rc = 0; const char * zLimit = NULL; fsl_cx * f = NULL; fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); if(FSL_RC_BREAK==rc) return 0; else if(rc) goto end; |
︙ | ︙ |
Changes to f-apps/f-vdiff.c.
︙ | ︙ | |||
385 386 387 388 389 390 391 | fsl_deck_finalize(&d1); fsl_deck_finalize(&d2); fsl_buffer_clear(&c1); fsl_buffer_clear(&c2); return rc; } | | | 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | fsl_deck_finalize(&d1); fsl_deck_finalize(&d2); fsl_buffer_clear(&c1); fsl_buffer_clear(&c2); return rc; } int main(int argc, char const * const * argv ){ int rc = 0; const char * vFrom = NULL; const char * vTo = NULL; const char * glob = NULL; const char * tmpStr = NULL; fsl_cx * f; fsl_id_t idFrom = -1, idTo = -1; |
︙ | ︙ |
Changes to f-apps/f-wiki.c.
︙ | ︙ | |||
259 260 261 262 263 264 265 | FCLI_V(("Crosslink callback for %s artifact [%.*s] (RID %"FSL_ID_T_PFMT")\n", fsl_satype_cstr(d->type), 8, d->uuid, d->rid)); return *((char const *)state) /* demonstrate what happens when crosslinking fails. */ ? FSL_RC_NYI : 0; } | | | 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | FCLI_V(("Crosslink callback for %s artifact [%.*s] (RID %"FSL_ID_T_PFMT")\n", fsl_satype_cstr(d->type), 8, d->uuid, d->rid)); return *((char const *)state) /* demonstrate what happens when crosslinking fails. */ ? FSL_RC_NYI : 0; } int main(int argc, char const * const * argv ){ int rc; fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); if(FSL_RC_BREAK==rc) /* --help */ return 0; else if(!rc){ if(!fsl_cx_db_repo(fcli.f)){ rc = fcli_err_set(FSL_RC_MISUSE, |
︙ | ︙ |
Changes to f-apps/f-zip.c.
︙ | ︙ | |||
46 47 48 49 50 51 52 | ++(*((int*)state)); if(vbose>1){ f_out("Adding: %.12s %s\n", fc->uuid, fc->name); } return 0; } | | | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | ++(*((int*)state)); if(vbose>1){ f_out("Adding: %.12s %s\n", fc->uuid, fc->name); } return 0; } int main(int argc, char const * const * argv ){ int rc = 0; const char * sym = NULL; const char * fileName = NULL; const char * rootDir = NULL; fsl_cx * f; fsl_db * db; char noRoot; |
︙ | ︙ |
Changes to f-apps/test.c.
︙ | ︙ | |||
347 348 349 350 351 352 353 | fsl_zip_finalize(&z); } static void fcli_local_help(){ puts("This is a scratchpad app and has no help."); } | | | 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | fsl_zip_finalize(&z); } static void fcli_local_help(){ puts("This is a scratchpad app and has no help."); } int main(int argc, char const * const * argv ){ int rc = 0; assert(sizeof(int32_t) >= 4); assert(sizeof(int64_t) >= 8); fcli.appHelp = fcli_local_help; rc = fcli_setup(argc, argv); if(rc) goto end; |
︙ | ︙ |
Changes to include/fossil-scm/fossil-cli.h.
1 2 3 4 5 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=2 et sw=2 tw=80: */ #if !defined(_NET_FOSSIL_SCM_FOSSILAPP_H_INCLUDED_) #define _NET_FOSSIL_SCM_FOSSILAPP_H_INCLUDED_ /* | | < | < | | < < < | < < < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=2 et sw=2 tw=80: */ #if !defined(_NET_FOSSIL_SCM_FOSSILAPP_H_INCLUDED_) #define _NET_FOSSIL_SCM_FOSSILAPP_H_INCLUDED_ /* Copyright 2013-2021 The Libfossil Authors, see LICENSES/BSD-2-Clause.txt SPDX-License-Identifier: BSD-2-Clause-FreeBSD SPDX-FileCopyrightText: 2021 The Libfossil Authors SPDX-ArtifactOfProjectName: Libfossil SPDX-FileType: Code ***************************************************************************** This file provides a basis for basic libfossil-using apps. It is intended to be used as the basis for test/demo apps, and not necessarily full-featured applications. */ |
︙ | ︙ | |||
151 152 153 154 155 156 157 158 159 160 161 162 163 164 | If not 0 then this is called after outputing any flags' help. It should output any additional help text using f_out(). */ void (*callback)(); }; typedef struct fcli_help_t fcli_help_t; /** The fcli_t type, accessed by clients via the ::fcli global instance, contains various data for managing a basic Fossil SCM application build using libfossil. Usage notes: | > > > > > > > > > > > > > > > > > > > > > > > > > | 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | If not 0 then this is called after outputing any flags' help. It should output any additional help text using f_out(). */ void (*callback)(); }; typedef struct fcli_help_t fcli_help_t; typedef struct fcli_flag_entry fcli_flag_entry; typedef int (*fcli_flag_callback_f)(fcli_flag_entry *); int fcli_flag_callback_f_not_again(fcli_flag_entry *); /** Under construction. Do not use. */ struct fcli_flag_entry { const char * flagShort; const char * flagLong; bool required; bool * flagBool; char const ** flagValue; fcli_flag_callback_f callback; const char * helpText; }; #define fcli_flag_entry_empty_m {0,0,false,NULL,NULL,NULL,NULL} FSL_EXPORT const fcli_flag_entry fcli_flag_entry_empty; #define FCLI_FLAG_BOOL_R(S,L,TGT,HELP) {S,L,true,TGT,NULL,NULL,HELP} #define FCLI_FLAG_BOOL(S,L,TGT,HELP) {S,L,false,TGT,NULL,NULL,HELP} #define FCLI_FLAG_R(S,L,TGT,HELP) {S,L,true,NULL,TGT,NULL,HELP} #define FCLI_FLAG(S,L,TGT,HELP) {S,L,false,NULL,TGT,NULL,HELP} #define FCLI_FLAG_X_R(S,L,TGT,CALLBACK,HELP) {S,L,true,NULL,TGT,CALLBACK,HELP} #define FCLI_FLAG_X(S,L,TGT,CALLBACK,HELP) {S,L,false,NULL,TGT,CALLBACK,HELP} //int fcli_process_flags( fcli_flag_entry * defs ); void fcli_help_via_flags(fcli_flag_entry const *defs); /** The fcli_t type, accessed by clients via the ::fcli global instance, contains various data for managing a basic Fossil SCM application build using libfossil. Usage notes: |
︙ | ︙ | |||
174 175 176 177 178 179 180 181 182 183 184 185 186 187 | app-specific help. Should be set by client applications BEFORE calling fcli_setup() so that the ::fcli help subsystem can integrate the app. fcli.appName will be set by the time this is called. */ void (*appHelp)(); /** If not NULL, it must be a pointer to fcli_help_t holding help info for the app. It will be formated and output when --help is triggered, in place of appHelp. */ fcli_help_t const * appHelp2; /** The shared fsl_cx instance. It gets initialized by | > > > > > > > > | 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | app-specific help. Should be set by client applications BEFORE calling fcli_setup() so that the ::fcli help subsystem can integrate the app. fcli.appName will be set by the time this is called. */ void (*appHelp)(); /** If set, it should be a brief string intended to be shown at the start of the help text immediate following by the binary's name. e.g. "file1...fileN" */ const char * briefUsage; /** DO NOT USE: this is being replaced. If not NULL, it must be a pointer to fcli_help_t holding help info for the app. It will be formated and output when --help is triggered, in place of appHelp. */ fcli_help_t const * appHelp2; /** The shared fsl_cx instance. It gets initialized by |
︙ | ︙ | |||
225 226 227 228 229 230 231 | /** True if the --dry-run flag is seen during initial arguments processing. ::fcli does not use this itself - it is intended as a convenience for applications. Those which want to support a short-form flag can implement that like this: @code | | | | | 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | /** True if the --dry-run flag is seen during initial arguments processing. ::fcli does not use this itself - it is intended as a convenience for applications. Those which want to support a short-form flag can implement that like this: @code bool dryRun = fcli.fDryRun ? fcli.fDryRun : fcli_flag("n", NULL); @endcode */ bool fDryRun; /** Transient settings and flags. These are bits which are used during (or very shortly after) fcli_setup() but have no effect if modified after that. */ struct { /** repo db name string from -R CLI flag. */ const char * repoDbArg; /** Set to true if fcli_setup() detects -? or --help in the argument list, or if the first non-flag argument is "help". */ bool helpRequested; /** Don't use this - it will likely go away. The following text was at some point true but is currently a lie: If the --gmt flag is found, this is set to true. That causes the FSL_CX_F_LOCALTIME_GMT flag to be set on this->f. */ |
︙ | ︙ | |||
331 332 333 334 335 336 337 | checking for a command after "help" in the latter case and showing command-specific help. Returns 0 on success. Errors other than FSL_RC_BREAK should be treated as fatal to the app, and fcli.f's error state _might_ contain info about the error. */ | | > > > > > | 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | checking for a command after "help" in the latter case and showing command-specific help. Returns 0 on success. Errors other than FSL_RC_BREAK should be treated as fatal to the app, and fcli.f's error state _might_ contain info about the error. */ FSL_EXPORT int fcli_setup(int argc, char const * const * argv ); /** Under construction. Do not use. */ FSL_EXPORT int fcli_setup2(int argc, char const * const * argv, fcli_flag_entry * flags); /** Returns the libfossil context associated with the fcli API. */ FSL_EXPORT fsl_cx * fcli_cx(); /** |
︙ | ︙ | |||
387 388 389 390 391 392 393 | exit. On success it removes the flag (and its value, if any) from fcli.argv. Thus by removing all flags early on, the CLI arguments are left only with non-flag arguments to sift through. Flags may start with either one or two dashes - they are equivalent. */ | | | > | | | | 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | exit. On success it removes the flag (and its value, if any) from fcli.argv. Thus by removing all flags early on, the CLI arguments are left only with non-flag arguments to sift through. Flags may start with either one or two dashes - they are equivalent. */ FSL_EXPORT bool fcli_flag(char const * opt, const char ** value); /** Works like fcli_flag() but tries two argument forms, in order. It is intended to be passed short and long forms, but can be passed two aliases or similar. It accepts NULL for either form. @code const char * v = NULL; fcli_flag2("n", "limit", &v); if(v) { ... } @endcode */ FSL_EXPORT bool fcli_flag2(char const * opt1, char const * opt2, const char ** value); /** Works similarly to fcli_flag2(), but if no flag is found and value is not NULL then *value is assigned to the return value of fcli_next_arg(1). In that case: - The return value will specify whether or not fcli_next_arg() returned a value or not. - If it returns true then *value is owned by fcli and will be cleaned up at app exit. - If it returns false, *value is not modified. The opt2 parameter may be NULL, but op1 may not. */ FSL_EXPORT bool fcli_flag_or_arg(char const * opt1, char const * opt2, const char ** value); /** Clears any error state in fcli.f. */ FSL_EXPORT void fcli_err_reset(); |
︙ | ︙ |
Changes to src/cli.c.
︙ | ︙ | |||
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include "fossil-scm/fossil-internal.h" #include "fossil-scm/fossil-cli.h" /** Convenience form of FCLI_VN for level-3 verbosity. */ #define FCLI_V3(pfexp) FCLI_VN(3,pfexp) #define fcli_empty_m { \ NULL/*appHelp*/, \ NULL/*appHelp2*/,\ NULL/*f*/, \ 0/*verbose*/, \ "."/*checkoutDir*/, \ NULL/*argv*/, \ 0/*argc*/, \ NULL/*appName*/, \ 0/*fDryRun*/, \ {/*transient*/ \ NULL/*repoDb*/, \ | > | | > | 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include "fossil-scm/fossil-internal.h" #include "fossil-scm/fossil-cli.h" /** Convenience form of FCLI_VN for level-3 verbosity. */ #define FCLI_V3(pfexp) FCLI_VN(3,pfexp) #define fcli_empty_m { \ NULL/*appHelp*/, \ NULL/*briefUsage*/, \ NULL/*appHelp2*/,\ NULL/*f*/, \ 0/*verbose*/, \ "."/*checkoutDir*/, \ NULL/*argv*/, \ 0/*argc*/, \ NULL/*appName*/, \ 0/*fDryRun*/, \ {/*transient*/ \ NULL/*repoDb*/, \ false/*helpRequested*/, \ 0/*gmtTime*/ \ }, \ {/*config*/ \ 0/*traceSql*/, \ fsl_outputer_empty_m \ } \ } const fcli_t fcli_empty = fcli_empty_m; fcli_t fcli = fcli_empty_m; const fcli_flag_entry fcli_flag_entry_empty = fcli_flag_entry_empty_m; void fcli_printf(char const * fmt, ...){ va_list args; va_start(args,fmt); if(fcli.f){ fsl_outputfv(fcli.f, fmt, args); }else{ |
︙ | ︙ | |||
226 227 228 229 230 231 232 | if(h->callback){ h->callback(); } }else if(fcli.appHelp){ fcli.appHelp(); } } | | | > | | | | | | | | | | | | | | | | | | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | | | | | 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | if(h->callback){ h->callback(); } }else if(fcli.appHelp){ fcli.appHelp(); } } static int fcli_process_argv( bool oldMode, int argc, char const * const * argv ){ int i; int rc = 0; char * cp; fcli.appName = argv[0]; fcli.argc = 0; fcli.argv = (char **)fsl_malloc( (argc + 1) * sizeof(char*)); fcli.argv[argc] = NULL; for( i = 1; i < argc; ++i ){ char const * arg = argv[i]; if('-'==*arg){ char const * flag = arg+1; while('-'==*flag) ++flag; #define FLAG(F) if(0==fsl_strcmp(F,flag)) if(oldMode){ FLAG("help") { fcli.transient.helpRequested = 1; continue; } FLAG("?") { fcli.transient.helpRequested = 1; continue; } FLAG("V") { fcli.verbose += 1; continue; } FLAG("VV") { fcli.verbose += 2; continue; } FLAG("VVV") { fcli.verbose += 3; continue; } FLAG("verbose") { fcli.verbose += 1; continue; } } #undef FLAG /* else fall through */ } cp = fsl_strdup(arg); if(!cp) return FSL_RC_OOM; fcli.argv[fcli.argc++] = cp; fcli_fax(cp); } /* Keep 'help' LAST so that we can can use the first-non-flag==help mechanism. */ if(fcli.argc && 0==fsl_strcmp("help",fcli.argv[0])){ fcli_next_arg(1) /* strip argument */; fcli.transient.helpRequested = 2; } return rc; } int fcli_flag_callback_f_not_again(fcli_flag_entry * x){ if(x){/*unused*/} return fcli_err_set(FSL_RC_MISUSE, "Flag may not be used a second time: %s%s%s", x->flagShort ? x->flagShort : "", (x->flagShort && x->flagLong) ? "/" : "", x->flagLong ? x->flagLong : ""); } static int fcli_flag_f_checkoutDir(fcli_flag_entry *f){ fcli.checkoutDir = *f->flagValue; return 0; } static fcli_flag_entry FcliFlags[] = { FCLI_FLAG_BOOL("?","help",&fcli.transient.helpRequested, "Show app help."), FCLI_FLAG_BOOL(0,"dry-run",&fcli.fDryRun, "Enable dry-run mode (not supported by all apps)."), FCLI_FLAG_X("C","no-checkout",NULL,fcli_flag_f_checkoutDir, "Disable automatic attempt to open checkout."), fcli_flag_entry_empty_m }; void fcli_help_via_flags(fcli_flag_entry const *defs){ fcli_flag_entry const * f; const char * tab = " "; for( f = defs; f->flagShort && f->flagLong; ++f ){ const char * s = f->flagShort; const char * l = f->flagLong; f_out("%s%s%s%s%s%s%s", tab, s?"-":"", s?s:"", (s&&l)?"|":"", l?"--":"",l?l:"", f->flagValue ? "=value" : ""); if(f->helpText){ f_out("\n%s%s%s", tab, tab, f->helpText); } f_out("\n"); } } static int fcli_process_flags( fcli_flag_entry * defs ) { fcli_flag_entry * f; int rc = 0; static bool once = false; if(!once){ once = true; rc = fcli_process_flags(FcliFlags); if(rc){ return rc; }else if(fcli.transient.helpRequested){ if(fcli.briefUsage){ f_out("Usage: %s [flags] %s\n", fcli.appName, fcli.briefUsage); }else{ f_out("Help for %s:\n", fcli.appName); } f_out("FCli global flags:\n"); fcli_help_via_flags(FcliFlags); f_out("\nApp-specific flags:\n"); fcli_help_via_flags(defs); if(fcli.appHelp){ f_out("\n"); fcli.appHelp(); } return FSL_RC_BREAK; } } for( f = defs; f->flagShort && f->flagLong; ++f ){ bool gotIt = fcli_flag2(f->flagShort, f->flagLong, f->flagValue ? f->flagValue : NULL); if(!gotIt && f->required){ const char * s = f->flagShort; const char * l = f->flagLong; rc = fcli_err_set(FSL_RC_MISUSE,"Missing required flag: %s%s%s\n", s?s:"", (s&&l)?"/":"", l?l:""); break; }else if(f->flagBool){ *f->flagBool = gotIt; } if(f->callback){ rc = f->callback(f); if(rc) break; } } return rc; } bool fcli_flag(char const * opt, const char ** value){ int i = 0; int remove = 0 /* number of items to remove from argv */; bool rc = false /* true if found, else 0 */; fsl_size_t optLen = fsl_strlen(opt); for( ; i < fcli.argc; ++i ){ char const * arg = fcli.argv[i]; char const * x; char const * vp = NULL; if(!arg || ('-' != *arg)) continue; rc = false; x = arg+1; if('-' == *x) { ++x;} if(0 != fsl_strncmp(x, opt, optLen)) continue; if(!value){ if(x[optLen]) continue /* not exact match */; /* Treat this as a boolean. */ rc = true; ++remove; break; }else{ /* -FLAG VALUE or -FLAG=VALUE */ if(x[optLen] == '='){ rc = true; vp = x+optLen+1; ++remove; } else if(x[optLen]) continue /* not an exact match */; else if(i<(fcli.argc-1)){ /* -FLAG VALUE */ rc = true; vp = fcli.argv[i+1]; remove += 2; } else{ /* --FLAG is expecting VALUE but we're at end of argv. Leave --FLAG in the args and report this as "not found." */ rc = false; assert(!remove); } if(rc){ *value = vp; } break; } |
︙ | ︙ | |||
374 375 376 377 378 379 380 | } fcli.argc -= remove; fcli.argv[i] = NULL; } return rc; } | | > | | > | | | | 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | } fcli.argc -= remove; fcli.argv[i] = NULL; } return rc; } bool fcli_flag2(char const * shortOpt, char const * longOpt, const char ** value){ bool rc = 0; if(shortOpt) rc = fcli_flag(shortOpt, value); if(!rc && longOpt) rc = fcli_flag(longOpt, value); return rc; } bool fcli_flag_or_arg(char const * shortOpt, char const * longOpt, const char ** value){ bool rc = fcli_flag(shortOpt, value); if(!rc){ rc = fcli_flag(longOpt, value); if(!rc && value){ const char * arg = fcli_next_arg(1); if(arg){ rc = true; *value = arg; } } } return rc; } |
︙ | ︙ | |||
427 428 429 430 431 432 433 | checking in an allocates-often API. */ static const fsl_allocator fcli_allocator = { fsl_realloc_f_failing, NULL/*state*/ }; | | < < | < < | > | | < < < < < | | > > > > | | | | | | | | | | | | | | | | | | | | | | | | | | < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | checking in an allocates-often API. */ static const fsl_allocator fcli_allocator = { fsl_realloc_f_failing, NULL/*state*/ }; static int fcli_setup_common1(bool oldMode, int argc, char const * const *argv){ static char once = 0; int rc = 0; if(once++){ fprintf(stderr,"MISUSE: fcli_setup() must " "not be called more than once."); return FSL_RC_MISUSE; } fslAllocOrig = fsl_lib_configurable.allocator; fsl_lib_configurable.allocator = fcli_allocator /* This MUST be done BEFORE the fsl API allocates ANY memory! */; atexit(fcli_shutdown); rc = fcli_process_argv(oldMode, argc, argv); return rc; } static int fcli_setup_common2(void){ int rc = 0; fsl_cx_init_opt init = fsl_cx_init_opt_empty; fsl_cx * f = 0; init.config.sqlPrint = 1; if(fcli.config.outputer.out){ init.output = fcli.config.outputer; fcli.config.outputer = fsl_outputer_empty /* To avoid any confusion about ownership */; }else{ init.output = fsl_outputer_FILE; init.output.state.state = stdout; } init.config.traceSql = fcli.config.traceSql; rc = fsl_cx_init( &f, &init ); fcli.f = f; FCLI_V3(("Initialized fsl_cx @0x%p. rc=%s\n", (void const *)f, fsl_rc_cstr(rc))); if(!rc){ #if 0 if(fcli.transient.gmtTime){ fsl_cx_flag_set(f, FSL_CX_F_LOCALTIME_GMT, 1); } #endif if(fcli.checkoutDir || fcli.transient.repoDbArg){ rc = fcli_open(); if(!fcli.transient.repoDbArg && fcli.checkoutDir && (FSL_RC_NOT_FOUND == rc)){ /* If [it looks like] we tried an implicit checkout-open but didn't find one, suppress the error. */ rc = 0; fcli_err_reset(); } } } if(!rc){ char const * userName = NULL; fcli_flag2("user", "U", &userName); if(userName){ fsl_cx_user_set(f, userName); }else if(!fsl_cx_user_get(f)){ char * u = fsl_guess_user_name(); fsl_cx_user_set(f, u); fsl_free(u); } } return rc; } int fcli_setup2(int argc, char const * const * argv, fcli_flag_entry * flags ){ int rc; rc = fcli_setup_common1(false, argc, argv); if(rc) return rc; rc = fcli_process_flags(flags); if(rc) return rc; rc = fcli_setup_common2(); return rc; } int fcli_setup(int argc, char const * const * argv ){ int rc = 0; rc = fcli_setup_common1(true, argc, argv); if(fcli.transient.helpRequested){ rc = FSL_RC_BREAK; }else{ rc = fcli_setup_common2(); } if(!rc){ if( fcli_flag2("C", "no-checkout", NULL) ){ fcli.checkoutDir = NULL; } fcli.config.traceSql = fcli_flag2("S","trace-sql", NULL); fcli.fDryRun = fcli_flag2("N","dry-run", NULL); fcli_flag2("R", "repo", &fcli.transient.repoDbArg); }else if(FSL_RC_BREAK==rc){ /* Do this last so that we can get the default user name and such for display in the help text. */ fcli_help(); } return rc; } void fcli_help_global_options(){ char const * user = fsl_cx_user_get(fcli.f); |
︙ | ︙ |