Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <stdexcept>
31 : : #include <osl/diagnose.h>
32 : : #include "cmdline.hxx"
33 : :
34 : : //---------------------------------
35 : : /** Simple command line abstraction
36 : : */
37 : :
38 : : // Creation
39 : :
40 : :
41 : 0 : CommandLine::CommandLine(size_t argc, char* argv[]) :
42 : : m_argc(argc),
43 : 0 : m_argv(argv)
44 : : {
45 : 0 : }
46 : :
47 : :
48 : : // Query
49 : :
50 : :
51 : : /** Returns an argument by name. If there are
52 : : duplicate argument names in the command line,
53 : : the first one wins.
54 : : Argument name an the argument value must be separated
55 : : by spaces. If the argument value starts with an
56 : : argument prefix use quotes else the return value is
57 : : an empty string because the value will be interpreted
58 : : as the next argument name.
59 : : If an argument value contains spaces use quotes.
60 : :
61 : : @precond GetArgumentNames() -> has element ArgumentName
62 : :
63 : : @throws std::invalid_argument exception
64 : : if the specified argument could not be
65 : : found
66 : : */
67 : 0 : std::string CommandLine::get_arg(const std::string& ArgumentName) const
68 : : {
69 : 0 : std::string arg_value;
70 : : size_t i;
71 : 0 : for ( i = 0; i < m_argc; i++)
72 : : {
73 : 0 : std::string arg = m_argv[i];
74 : :
75 : 0 : if (ArgumentName == arg && ((i+1) < m_argc) && !is_arg_name(m_argv[i+1]))
76 : : {
77 : 0 : arg_value = m_argv[i+1];
78 : : break;
79 : : }
80 : 0 : }
81 : :
82 : 0 : if (i == m_argc)
83 : 0 : throw std::invalid_argument("Invalid argument name");
84 : :
85 : 0 : return arg_value;
86 : : }
87 : :
88 : :
89 : : // Command
90 : :
91 : :
92 : : /** Returns whether a given argument is an argument name
93 : : */
94 : 0 : bool CommandLine::is_arg_name(const std::string& Argument) const
95 : : {
96 : 0 : return (Argument.length() > 0 && Argument[0] == '-');
97 : : }
98 : :
99 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|