Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Configuration of minimist #553

Closed
sstendal opened this issue Nov 24, 2022 · 8 comments · Fixed by #661
Closed

Feature request: Configuration of minimist #553

sstendal opened this issue Nov 24, 2022 · 8 comments · Fixed by #661

Comments

@sstendal
Copy link

It is currently difficult to use the minimist integration if you have a combination of boolean arguments and unnamed arguments.

This example script will fail if the -h option is placed before the filename parameter

#!/usr/bin/env zx

$.verbose = false

const header = argv.header || argv.h
const filename = argv._[0]

if (header) {
  console.log('Filename: ', filename)
}

$`cat ${filename}`.pipe(process.stdout)
// This will fail, since 'myfile.txt' will be interpreted as the value of -h
myscript.mjs -h myfile.txt

// This will work as expected
myscript.mjs myfile.txt -h 

A solution to this would be either:

A) A possibility to provide a configuration for minimist.

B) Expose the minimist package to the script, so that the script writer may call it directly and provide a configuration

I believe A) would be better, while B) would be easier to implement

@antonmedv
Copy link
Collaborator

B) Expose the minimist package to the script, so that the script writer may call it directly and provide a configuration

This option is already available. Just import minimist.

@sstendal
Copy link
Author

Maybe I misunderstand this, but importing minimist as an external package will require that minimist is available as an external package for everyone that should run the script, or run zx with the --install parameter.

If I just add this line to the script above, and does not run zx with --install, then I get an error.

import minimist from 'minimist'

The error message (as expected, I believe)

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'minimist' ...

Since minimist is already imported by zx, maybe it would be possible to expose minimist to the script as a global variable in the same way as chalk, which, YAML etc.?

@antonmedv
Copy link
Collaborator

I see, script out of node_modules folder. Yes, this will require -install flag.

@aarondill
Copy link

A few ideas on how to implement this:

  1. Expose the minimist module as a global minimist
  2. Expose the minimist.parseArgs function as a global parseArgs
  3. Modify the updateArgv function in src/goods.ts to accept an optional options argument and expose it.

@ReeganExE
Copy link

ReeganExE commented Apr 8, 2023

Since we need to configure our program to accept both long and short arguments (-e/--expression), it would be helpful if updateArgv accepted minimist options, allowing us to add aliases to minimist.

zx/src/goods.ts

Lines 33 to 36 in 101b810

export function updateArgv(args: string[]) {
argv = minimist(args)
;(global as any).argv = argv
}

Alternatively, developers can import 'minimist' and parse arguments themselves, but it can result in another issue with ES Modules: #553 (comment) and #595 (comment). So import minimist from 'minimist' will not work (at runtime) in ES Module mode as mentioned in the referenced comment.

Regarding the --install option, why do we have to reinstall minimist while it is already bundled with zx?

@antonmedv
Copy link
Collaborator

Good point. Let’s expose minimist.

@alpap
Copy link

alpap commented Jul 14, 2023

Any update on this?
is the package exposed now?

@antonmedv
Copy link
Collaborator

No update.

@LionC LionC mentioned this issue Aug 14, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants