Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <stdexcept>
22 : #include <osl/diagnose.h>
23 : #include "cmdline.hxx"
24 :
25 : //---------------------------------
26 : /** Simple command line abstraction
27 : */
28 :
29 : // Creation
30 :
31 :
32 0 : CommandLine::CommandLine(size_t argc, char* argv[]) :
33 : m_argc(argc),
34 0 : m_argv(argv)
35 : {
36 0 : }
37 :
38 :
39 : // Query
40 :
41 :
42 : /** Returns an argument by name. If there are
43 : duplicate argument names in the command line,
44 : the first one wins.
45 : Argument name an the argument value must be separated
46 : by spaces. If the argument value starts with an
47 : argument prefix use quotes else the return value is
48 : an empty string because the value will be interpreted
49 : as the next argument name.
50 : If an argument value contains spaces use quotes.
51 :
52 : @precond GetArgumentNames() -> has element ArgumentName
53 :
54 : @throws std::invalid_argument exception
55 : if the specified argument could not be
56 : found
57 : */
58 0 : std::string CommandLine::get_arg(const std::string& ArgumentName) const
59 : {
60 0 : std::string arg_value;
61 : size_t i;
62 0 : for ( i = 0; i < m_argc; i++)
63 : {
64 0 : std::string arg = m_argv[i];
65 :
66 0 : if (ArgumentName == arg && ((i+1) < m_argc) && !is_arg_name(m_argv[i+1]))
67 : {
68 0 : arg_value = m_argv[i+1];
69 : break;
70 : }
71 0 : }
72 :
73 0 : if (i == m_argc)
74 0 : throw std::invalid_argument("Invalid argument name");
75 :
76 0 : return arg_value;
77 : }
78 :
79 :
80 : // Command
81 :
82 :
83 : /** Returns whether a given argument is an argument name
84 : */
85 0 : bool CommandLine::is_arg_name(const std::string& Argument) const
86 : {
87 0 : return (Argument.length() > 0 && Argument[0] == '-');
88 : }
89 :
90 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|