我们正在使用电子包装器来捆绑和分发我们的 Web 应用程序的前端。我们需要能够将服务器的host和传递port到电子前端进行连接。当我们通过electron main.js --host blah --port 8080它启动时。打包后,我们运行通过./MyApp --host blah --port 8080,它不起作用。这很糟糕,因为我们不希望客户需要自己安装电子/npm。另外值得注意的是,无论我们是否将应用程序打包到asar存档中,都会发生这种情况。
关于我们可以尝试的任何想法,或者我们是否试图以错误的方式解决这个问题?
这是我尝试过的(代码改编自yargs github自述文件中示例的代码):
// main.ts
import {Argv} from "yargs";
console.info(`CLI starter.`);
function serve(port: string) {
console.info(`Serve on port ${port}.`);
}
require('yargs')
.command('serve', "Start the server.", (yargs: Argv) => {
yargs.option('port', {
describe: "Port to bind on",
default: "5000",
}).option('verbose', {
alias: 'v',
default: false,
})
}, (args: any) => {
if (args.verbose) {
console.info("Starting the server...");
}
serve(args.port);
}).argv;
Run Code Online (Sandbox Code Playgroud)
结果:
npm run-script build; node build/main.js --port=432 --verbose
> typescript-cli-starter@0.0.1 build /Users/kaiyin/WebstormProjects/typescript-cli-starter
> tsc -p .
CLI starter.
Run Code Online (Sandbox Code Playgroud)
看起来yargs在这里没有任何作用。
任何想法如何使它工作?
假设我想向我的(启用 yargs 的)脚本传递一个布尔选项:
./foo --secure-but-slow=false
Run Code Online (Sandbox Code Playgroud)
secure-but-slow应默认为 true,因此只有知道自己在做什么的用户才会选择不安全的选项。然而,对于高级用户来说,有一个快捷方式的短选项标志会很好,例如
./foo -i
Run Code Online (Sandbox Code Playgroud)
-i告诉 yargs应该设置的正确方法是什么--secure-but-slow=false?
我正在使用yargs获取CLI参数。我想知道命令和选项之间的区别。
const argv = yargs
.command(
'add',
'Add a new note',
{
title: titleOptions,
body: bodyOptions
})
.argv;
Run Code Online (Sandbox Code Playgroud)
和
const argv = yargs
.option('address', {
alias: 'a',
demand: true,
describe: 'Address for fetching weather'
})
.help()
.alias('help', 'h')
.argv
Run Code Online (Sandbox Code Playgroud) 我正在使用 yargs 来收集传递的任何参数。我的场景是能够传递带有双引号的空格的字符串,如下所示:
./nodeFile.js -n "my node module"
Run Code Online (Sandbox Code Playgroud)
但它实际上将 的"my参数值作为yargs.argv.n并将剩余的字符串放入 中_。我知道当我们使用转义字符时它会起作用,"但我不能假设这对我的用户来说是显而易见的。
谁能帮我看看是否有任何配置或任何黑客可以使用它来收集整个字符串?
我需要解释两点 - 如何在不使用已弃用的 .reset() 方法的情况下重置 yargs;(.global() 方法,但如何??) - 如何将命令行传递给用户输入的 yargs
鉴于以下测试:
import * as yargs from 'yargs';
describe.only('how to write a unit test for yargs with typescript', () => {
beforeEach(() => {
yargs.reset(); // No, but how do we get a new yargs instance?
});
it('yargs', () => {
const usernameOption = {
alias: 'username',
describe: 'website username'
};
// (['login', '-u', 'jimmy']) => where does this go, without causing a build error
// TS2349: This expression is not …Run Code Online (Sandbox Code Playgroud) 我有这个代码
const argv = yargs
.option("applyChanges", {
alias: "a",
description: "Apply the changes",
type: "boolean"
})
.help()
.alias("help", "h").argv;
const options = {
applyChanges: argv.applyChanges ? argv.applyChanges : false
};
Run Code Online (Sandbox Code Playgroud)
获取argv.applyChanges布尔值。但在最新更新 yargs 17 之后,我收到一条错误消息argv.applyChanges:
属性“applyChanges”在类型“{[x:字符串]:未知;applyChanges: 布尔值 | 不明确的; _: (字符串 | 数字)[]; $0:字符串;} | Promise<{ [x: string]: 未知; applyChanges: 布尔值 | 不明确的; _: (字符串 | 数字)[]; $0:字符串;}>'。类型“Promise<{[x: string]:unknown;”上不存在属性“applyChanges” applyChanges: 布尔值 | 不明确的; _: (字符串 | 数字)[]; $0:字符串;}>'。
我尝试使用await但没有成功。我应该怎么办?此代码适用于之前的 yargs 版本 16.xx
我正在尝试运行该yargs.command命令。我尝试运行这个代码片段:
import yargs from "yargs";
yargs.command({
command: "list",
describe: "List all commands",
handler: function () {
console.log("Listing all commands.");
},
});
yargs.command({
command: "read",
describe: "Reading all commands",
handler: function () {
console.log("Reading all commands");
},
});
Run Code Online (Sandbox Code Playgroud)
我在输出中收到此错误:
TypeError: yargs.command is not a function
at file:///media/Main-Volume/courses/Udemy%20courses/Node%20JS%20bootcamp/notes-app/app.js:23:7
at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
at async Loader.import (internal/modules/esm/loader.js:182:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
Run Code Online (Sandbox Code Playgroud)
在网上搜索后,我找到了这个解决方案,并在我的代码末尾添加了这样的语句:yargs.parse()。但不幸的是,我仍然得到相同的输出。
我的操作系统:MX-Linux 21。
节点:12.22.5。
亚格斯:17.4.1。
VS 代码:1.66.2。
有谁知道出了什么问题吗?任何帮助,将不胜感激。
我正在尝试用打字稿中的 yargs 解析命令行,但它不起作用:
import yargs from 'yargs';
const argv = yargs
.option('label', {
alias: 'l',
describe: 'Execute bot with these labels',
demandOption: false,
type: 'string',
})
.option('console', {
alias: 'c',
describe: 'Log to console',
demandOption: false,
type: 'boolean',
})
.help()
.alias('help', 'h')
.argv;
if(argv.label){
console.log('label')
}
Run Code Online (Sandbox Code Playgroud)
编译器抛出并错误:
Property 'label' does not exist on type '{ [x: string]: unknown; label: string | undefined; console: boolean | undefined; _: (string | number)[]; $0: string; } | Promise<{ [x: string]: unknown; label: string …Run Code Online (Sandbox Code Playgroud) yargs ×10
node.js ×6
typescript ×3
javascript ×2
atom-editor ×1
electron ×1
npm ×1
unit-testing ×1