summaryrefslogtreecommitdiffstats
path: root/xbmc/application/AppParamParser.cpp
blob: ca9cd2acef32cc922bfedd3d114ad3fe4fc83255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "AppParamParser.h"

#include "CompileInfo.h"
#include "FileItem.h"
#include "ServiceBroker.h"
#include "application/AppParams.h"
#include "utils/StringUtils.h"
#include "utils/SystemInfo.h"
#include "utils/log.h"

#include <iostream>
#include <stdlib.h>
#include <string>

namespace
{

constexpr const char* versionText =
    R"""({0} Media Center {1}
Copyright (C) {2} Team {0} - http://kodi.tv
)""";

constexpr const char* helpText =
    R"""(Usage: {0} [OPTION]... [FILE]...

Arguments:
  -fs                   Runs {1} in full screen
  --standalone          {1} runs in a stand alone environment without a window
                        manager and supporting applications. For example, that
                        enables network settings.
  -p or --portable      {1} will look for configurations in install folder instead of ~/.{0}
  --debug               Enable debug logging
  --version             Print version information
  --test                Enable test mode. [FILE] required.
  --settings=<filename> Loads specified file after advancedsettings.xml replacing any settings specified
                        specified file must exist in special://xbmc/system/
)""";

} // namespace

CAppParamParser::CAppParamParser() : m_params(std::make_shared<CAppParams>())
{
}

void CAppParamParser::Parse(const char* const* argv, int nArgs)
{
  std::vector<std::string> args;
  args.reserve(nArgs);

  for (int i = 0; i < nArgs; i++)
  {
    args.emplace_back(argv[i]);
    if (i > 0)
      ParseArg(argv[i]);
  }

  if (nArgs > 1)
  {
    // testmode is only valid if at least one item to play was given
    if (m_params->GetPlaylist().IsEmpty())
      m_params->SetTestMode(false);
  }

  // Record raw paramerters
  m_params->SetRawArgs(std::move(args));
}

void CAppParamParser::DisplayVersion()
{
  std::cout << StringUtils::Format(versionText, CSysInfo::GetAppName(), CSysInfo::GetVersion(),
                                   CCompileInfo::GetCopyrightYears());
  exit(0);
}

void CAppParamParser::DisplayHelp()
{
  std::string lcAppName = CSysInfo::GetAppName();
  StringUtils::ToLower(lcAppName);

  std::cout << StringUtils::Format(helpText, lcAppName, CSysInfo::GetAppName());
}

void CAppParamParser::ParseArg(const std::string &arg)
{
  if (arg == "-fs" || arg == "--fullscreen")
    m_params->SetStartFullScreen(true);
  else if (arg == "-h" || arg == "--help")
  {
    DisplayHelp();
    exit(0);
  }
  else if (arg == "-v" || arg == "--version")
    DisplayVersion();
  else if (arg == "--standalone")
    m_params->SetStandAlone(true);
  else if (arg == "-p" || arg  == "--portable")
    m_params->SetPlatformDirectories(false);
  else if (arg == "--debug")
    m_params->SetLogLevel(LOG_LEVEL_DEBUG);
  else if (arg == "--test")
    m_params->SetTestMode(true);
  else if (arg.substr(0, 11) == "--settings=")
    m_params->SetSettingsFile(arg.substr(11));
  else if (arg.length() != 0 && arg[0] != '-')
  {
    const CFileItemPtr item = std::make_shared<CFileItem>(arg);
    item->SetPath(arg);
    m_params->GetPlaylist().Add(item);
  }
}