Skip to content

Commit

Permalink
Fix flag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow committed Jan 20, 2021
1 parent 52632a8 commit 2c97876
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"bencode": "^2.0.1",
"chalk": "^4.1.0",
"commander": "^5.1.0",
"commander": "^7.0.0",
"parse-torrent": "^7.1.3",
"simple-get": "^4.0.0",
"xmlrpc": "^1.3.2"
Expand Down
56 changes: 31 additions & 25 deletions src/cmd.js
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const { program, Command } = require("commander");
const { program, Command, Option } = require("commander");
const chalk = require("chalk");
const packageDotJson = require("../package.json");
const { main } = require("./index");
Expand All @@ -12,16 +12,27 @@ const logger = require("./logger");
require("./signalHandlers");
const { ACTIONS } = require("./constants");

function fallback(...args) {
for (const arg of args) {
if (arg !== undefined) return arg;
}
return undefined;
}

async function run() {
const fileConfig = getFileConfig();

function fallback(...args) {
for (const arg of args) {
if (arg !== undefined) return arg;
}
return undefined;
}

function processOptions(options) {
options.trackers = options.trackers.split(",").filter((e) => e !== "");
if (options.action === "inject" && !options.rtorrentRpcUrl) {
logger.error(
"You need to specify --rtorrent-rpc-url when using '-A inject'."
);
process.exit(1);
}
return options;
}

function addSharedOptions() {
return this.requiredOption(
"-u, --jackett-server-url <url>",
Expand Down Expand Up @@ -56,12 +67,15 @@ async function run() {
"Search for all torrents regardless of their contents",
fallback(fileConfig.searchAll, false)
)

.requiredOption("-v, --verbose", "Log verbose output", false)
.requiredOption(
"-A, --action <action>",
"If set to 'inject', cross-seed will attempt to add the found torrents to your torrent client.",
fallback(fileConfig.action, ACTIONS.SAVE)
.addOption(
new Option(
"-A, --action <action>",
"If set to 'inject', cross-seed will attempt to add the found torrents to your torrent client."
)
.default(fallback(fileConfig.action, ACTIONS.SAVE))
.choices(Object.values(ACTIONS))
.makeOptionMandatory()
)
.option(
"--rtorrent-rpc-url <url>",
Expand Down Expand Up @@ -102,12 +116,8 @@ async function run() {
.command("daemon")
.description("Start the cross-serve daemon")
.addSharedOptions()
.action(async (command) => {
const options = command.opts();
options.trackers = options.trackers
.split(",")
.filter((e) => e !== "");
setRuntimeConfig(options);
.action(async (options, _command) => {
setRuntimeConfig(processOptions(options));
try {
if (process.env.DOCKER_ENV === "true") {
generateConfig({ docker: true });
Expand Down Expand Up @@ -149,12 +159,8 @@ async function run() {
"Exclude torrents which have been searched more recently than x minutes ago. Overrides the -a flag.",
(n) => parseInt(n)
)
.action(async (command) => {
const options = command.opts();
options.trackers = options.trackers
.split(",")
.filter((e) => e !== "");
setRuntimeConfig(options);
.action(async (options, _command) => {
setRuntimeConfig(processOptions(options));
try {
await main();
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/constants.js
Expand Up @@ -10,8 +10,8 @@ const README_URL = "https://github.com/mmgoodnow/cross-seed";
const DAEMON_MODE_URL_HASH = "#daemon-mode-rtorrent-only-docker-recommended";

const ACTIONS = {
SAVE: "SAVE",
INJECT: "INJECT",
SAVE: "save",
INJECT: "inject",
};

// because I'm sick of intellij whining at me
Expand Down

0 comments on commit 2c97876

Please sign in to comment.