1*6b3a42afSjmmv // Copyright 2010 Google Inc.
2*6b3a42afSjmmv // All rights reserved.
3*6b3a42afSjmmv //
4*6b3a42afSjmmv // Redistribution and use in source and binary forms, with or without
5*6b3a42afSjmmv // modification, are permitted provided that the following conditions are
6*6b3a42afSjmmv // met:
7*6b3a42afSjmmv //
8*6b3a42afSjmmv // * Redistributions of source code must retain the above copyright
9*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer.
10*6b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright
11*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer in the
12*6b3a42afSjmmv // documentation and/or other materials provided with the distribution.
13*6b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*6b3a42afSjmmv // may be used to endorse or promote products derived from this software
15*6b3a42afSjmmv // without specific prior written permission.
16*6b3a42afSjmmv //
17*6b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*6b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*6b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*6b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*6b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*6b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*6b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*6b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*6b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*6b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*6b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*6b3a42afSjmmv
29*6b3a42afSjmmv #include "utils/cmdline/globals.hpp"
30*6b3a42afSjmmv
31*6b3a42afSjmmv #include "utils/format/macros.hpp"
32*6b3a42afSjmmv #include "utils/logging/macros.hpp"
33*6b3a42afSjmmv #include "utils/fs/path.hpp"
34*6b3a42afSjmmv #include "utils/sanity.hpp"
35*6b3a42afSjmmv
36*6b3a42afSjmmv namespace cmdline = utils::cmdline;
37*6b3a42afSjmmv
38*6b3a42afSjmmv namespace {
39*6b3a42afSjmmv
40*6b3a42afSjmmv
41*6b3a42afSjmmv /// The name of the binary used to execute the program.
42*6b3a42afSjmmv static std::string Progname;
43*6b3a42afSjmmv
44*6b3a42afSjmmv
45*6b3a42afSjmmv } // anonymous namespace
46*6b3a42afSjmmv
47*6b3a42afSjmmv
48*6b3a42afSjmmv /// Initializes the global state of the CLI.
49*6b3a42afSjmmv ///
50*6b3a42afSjmmv /// This function can only be called once during the execution of a program,
51*6b3a42afSjmmv /// unless override_for_testing is set to true.
52*6b3a42afSjmmv ///
53*6b3a42afSjmmv /// \param argv0 The value of argv[0]; i.e. the program name.
54*6b3a42afSjmmv /// \param override_for_testing Should always be set to false unless for tests
55*6b3a42afSjmmv /// of this functionality, which may set this to true to redefine internal
56*6b3a42afSjmmv /// state.
57*6b3a42afSjmmv void
init(const char * argv0,const bool override_for_testing)58*6b3a42afSjmmv cmdline::init(const char* argv0, const bool override_for_testing)
59*6b3a42afSjmmv {
60*6b3a42afSjmmv if (!override_for_testing)
61*6b3a42afSjmmv PRE_MSG(Progname.empty(), "cmdline::init called more than once");
62*6b3a42afSjmmv Progname = utils::fs::path(argv0).leaf_name();
63*6b3a42afSjmmv LD(F("Program name: %s") % Progname);
64*6b3a42afSjmmv POST(!Progname.empty());
65*6b3a42afSjmmv }
66*6b3a42afSjmmv
67*6b3a42afSjmmv
68*6b3a42afSjmmv /// Gets the program name.
69*6b3a42afSjmmv ///
70*6b3a42afSjmmv /// \pre init() must have been called in advance.
71*6b3a42afSjmmv ///
72*6b3a42afSjmmv /// \return The program name.
73*6b3a42afSjmmv const std::string&
progname(void)74*6b3a42afSjmmv cmdline::progname(void)
75*6b3a42afSjmmv {
76*6b3a42afSjmmv PRE_MSG(!Progname.empty(), "cmdline::init not called yet");
77*6b3a42afSjmmv return Progname;
78*6b3a42afSjmmv }
79