Repository: electron/electron
File: shell/common/api/electron_api_command_line.cc (C++)

Code before:
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"

namespace {

// Chromium will hard CHECK if the switch name is not lowercase.
bool IsSwitchNameValid(std::string_view switch_name) {
  return base::ToLowerASCII(switch_name) == switch_name;
}

bool HasSwitch(gin_helper::ErrorThrower thrower,
               const std::string& switch_string) {
  if (!IsSwitchNameValid(switch_string)) {
    thrower.ThrowError("Switch name must be lowercase");
    return false;
  }

  return base::CommandLine::ForCurrentProcess()->HasSwitch(switch_string);
}

base::CommandLine::StringType GetSwitchValue(gin_helper::ErrorThrower thrower,
                                             const std::string& switch_string) {
  if (!IsSwitchNameValid(switch_string)) {
    thrower.ThrowError("Switch name must be lowercase");
    return base::CommandLine::StringType();
  }

  return base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
      switch_string);
}

void AppendSwitch(const std::string& switch_string,
                  gin_helper::Arguments* args) {
  if (!IsSwitchNameValid(switch_string)) {
    args->ThrowError("Switch name must be lowercase");
    return;
  }

  auto* command_line = base::CommandLine::ForCurrentProcess();
  if (base::EndsWith(switch_string, "-path",
                     base::CompareCase::INSENSITIVE_ASCII) ||
      switch_string == network::switches::kLogNetLog) {
    base::FilePath path;
    args->GetNext(&path);
    command_line->AppendSwitchPath(switch_string, path);
    return;
  }

  base::CommandLine::StringType value;
  if (args->GetNext(&value))
    command_line->AppendSwitchNative(switch_string, value);

Review comment (suggestion):
Following up on the `CheckSwitchNameValid()` suggestion:

```suggestion
  if (const auto errmsg = CheckSwitchNameValid(switch_string)) {
    thrower.ThrowError(*errmsg);
    return {};
  }
```

Code after:
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"

namespace {
bool HasSwitch(const std::string& switch_string) {
  auto switch_str = base::ToLowerASCII(switch_string);

  auto* command_line = base::CommandLine::ForCurrentProcess();
  return command_line->HasSwitch(switch_str);
}

base::CommandLine::StringType GetSwitchValue(gin_helper::ErrorThrower thrower,
                                             const std::string& switch_string) {
  auto switch_str = base::ToLowerASCII(switch_string);

  auto* command_line = base::CommandLine::ForCurrentProcess();
  return command_line->GetSwitchValueNative(switch_str);
}

void AppendSwitch(const std::string& switch_string,
                  gin_helper::Arguments* args) {
  auto switch_str = base::ToLowerASCII(switch_string);
  auto* command_line = base::CommandLine::ForCurrentProcess();
  if (base::EndsWith(switch_string, "-path",
                     base::CompareCase::INSENSITIVE_ASCII) ||
      switch_string == network::switches::kLogNetLog) {
    base::FilePath path;
    args->GetNext(&path);
    command_line->AppendSwitchPath(switch_str, path);
    return;
  }

  base::CommandLine::StringType value;
  if (args->GetNext(&value))
    command_line->AppendSwitchNative(switch_str, value);
  else
    command_line->AppendSwitch(switch_str);
}

void RemoveSwitch(const std::string& switch_string) {
  auto switch_str = base::ToLowerASCII(switch_string);

  auto* command_line = base::CommandLine::ForCurrentProcess();
  command_line->RemoveSwitch(switch_str);
}

void AppendArg(const std::string& arg) {
  auto* command_line = base::CommandLine::ForCurrentProcess();
  command_line->AppendArg(arg);
}