LibreOffice Module shell (master) 1
cmdline.cxx
Go to the documentation of this file.
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#include <stdexcept>
21#include "cmdline.hxx"
22
26// Creation
27
28CommandLine::CommandLine(size_t argc, char* argv[])
29 : m_argc(argc)
30 , m_argv(argv)
31{
32}
33
34// Query
35
52std::string CommandLine::get_arg(const std::string& ArgumentName) const
53{
54 std::string arg_value;
55 size_t i;
56 for (i = 0; i < m_argc; i++)
57 {
58 std::string arg = m_argv[i];
59
60 if (ArgumentName == arg && ((i + 1) < m_argc) && !is_arg_name(m_argv[i + 1]))
61 {
62 arg_value = m_argv[i + 1];
63 break;
64 }
65 }
66
67 if (i == m_argc)
68 throw std::invalid_argument("Invalid argument name");
69
70 return arg_value;
71}
72
73// Command
74
77bool CommandLine::is_arg_name(const std::string& Argument)
78{
79 return (Argument.length() > 0 && Argument[0] == '-');
80}
81
82/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
char ** m_argv
Definition: cmdline.hxx:64
std::string get_arg(const std::string &ArgumentName) const
Returns an argument by name.
Definition: cmdline.cxx:52
static bool is_arg_name(const std::string &Argument)
Returns whether a given argument is an argument name.
Definition: cmdline.cxx:77
CommandLine(size_t argc, char *argv[])
Simple command line abstraction.
Definition: cmdline.cxx:28
size_t m_argc
Definition: cmdline.hxx:63
int i