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

Code before:
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#include "services/network/public/cpp/network_switches.h"
#include "shell/common/gin_converters/base_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#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)) {

Review comment (suggestion):
Couplea suggestions here:

- It's better to put the "Switch name must be lowercase" idea here so that the other methods don't have to know what this function's internal logic is
- We can test for lowercase without making a new `std::string`


```suggestion
std::optional<std::string_view> CheckSwitchNameValid(std::string_view key) {
  // Chromium will hard CHECK if the switch name is not lowercase.
  if (!std::ranges::none_of(key, absl::ascii_isupper))
    return "Switch name must be lowercase";

  return {};
 }
```


Code after:
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#include "services/network/public/cpp/network_switches.h"
#include "shell/common/gin_converters/base_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#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;