1 /**
2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */10 moduledextool.plugin.analyze.raw_config;
11 12 importlogger = std.experimental.logger;
13 14 /** Handle parsing of user arguments.
15 16 For a simple plugin this is overly complex. But a plugin very seldom stays
17 simple. By keeping the user input parsing and validation separate from the rest
18 of the program it become more robust to future changes.
19 */20 structRawConfiguration {
21 importstd.getopt : GetoptResult, getopt, defaultGetoptPrinter;
22 23 boolhelp;
24 boolerrorHelp;
25 boolshortPluginHelp;
26 boolmccabe;
27 booloutputJson;
28 booloutputStdout;
29 intmccabeThreshold = 5;
30 intworkerThreads = -1;
31 stringoutdir = ".";
32 stringrestrictDir = ".";
33 string[] cflags;
34 string[] compileDb;
35 string[] files;
36 37 privateGetoptResulthelp_info;
38 39 voidparse(string[] args) @safe {
40 staticimportstd.getopt;
41 42 try {
43 // getopt is safe in newer versions than 2.071.144 // remove this trusted wrapper there.45 // dfmt off46 () @trusted {
47 help_info = getopt(args, std.getopt.config.keepEndOfOptions,
48 "short-plugin-help", "short description of the plugin", &shortPluginHelp,
49 "threads", "number of worker threads to use (default: detected CPU cores)", &workerThreads,
50 "compile-db", "Retrieve compilation parameters from the file", &compileDb,
51 "output-json", "Write the analyze result to json file(s)", &outputJson,
52 "output-stdout", "Write the analyze result to stdout", &outputStdout,
53 "restrict", "Restrict analyze of files to those in this directory tree (default: .)", &restrictDir,
54 "mccabe", "Calculate the McCabe complexity of functions and files", &mccabe,
55 "mccabe-threshold", "Threshold that must be reached for the McCabe value to be reported (default: 5)", &mccabeThreshold,
56 "out", "directory to write result files to (default: .)", &outdir,
57 "in", "Input file to parse", &files,
58 );
59 }();
60 // dfmt on61 help = help_info.helpWanted;
62 }
63 catch (std.getopt.GetOptExceptionex) {
64 logger.error(ex.msg);
65 errorHelp = true;
66 }
67 68 importstd.algorithm : find;
69 importstd.array : array;
70 importstd.range : drop;
71 72 // at this point args contain "what is left". What is interesting then is those after "--".73 cflags = args.find("--").drop(1).array();
74 75 if (!shortPluginHelp) {
76 // DMD-2.075.077 // workaround because the log is funky. those from78 // application.plugin.toPlugin aren't "flushed". It think it is79 // because pipeProcess uses linux pipes and this somehow interacts80 // badly with std.experimental.logger;81 debuglogger.trace("");
82 83 debuglogger.trace(this);
84 }
85 }
86 87 voidprintHelp() @trusted {
88 importstd.stdio : writeln;
89 90 defaultGetoptPrinter("Usage: dextool analyze [options] [--in=] [-- CFLAGS...]",
91 help_info.options);
92 }
93 }