Attempting to utilize the Optimist API's help() function to display the usage() information

I am relatively new to using optimist and despite my attempts at researching and experimenting, I have been unable to find a streamlined method for incorporating a --help option.

In the documentation, there is mention of a help() function. Based on this information, I would anticipate that the following code should function as intended:

var argv = require('optimist')
  .usage('Some usage')
  .alias('l', 'local')
  .describe('l', 'uses local repo')
  .help()
  .argv

If I were to enter ./myScript --help in the terminal, I would expect to see the specified usage information. While I understand that I could examine the argv object for a -h or --help option and then use console(argv.usage) to display the usage, my goal was to utilize the provided API instead of implementing a workaround.

Does this question align with what is expected? Any assistance would be greatly appreciated.

bitoiu

Answer №1

If you are looking to show usage information, it's important to keep a reference to the object returned from require(). This is because the object received from .argv is a plain object and does not have direct access to functions like help() or showHelp(). Below is an example to guide you in the right direction:

var optimist = require('optimist')
    .usage('$0: This is an example on how to use optimist')
    .describe('h', 'Display the usage')
    .describe('l', 'uses local repo')
    .alias('h', 'help')
    .alias('l', 'local');

var argv = optimist.argv;

if (argv.help) {
    optimist.showHelp();
    process.exit(0);
}

if (argv.local) {
    // Perform actions based on local repository
    console.info('Got the -l/--local flag!');
}

Running the code with either -h or --help as arguments will display the following output:

node ./ex-optimist.js: This is an example on how to use optimist

Options:
  -h, --help   Display the usage
  -l, --local  uses local repo

It's worth noting that using the .help function will return the usage as a string, and subsequent attempts to use .argv will result in your argv variable being 'undefined'.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Parcel JS: The function tree.render is missing or not defined

Every time I attempt to execute the production build command npm run build or npx parcel build index.html, this error occurs. My project consists of simple HTML and CSS, without any React or third-party libraries. What could be causing this issue? I have t ...

Can you guide me on the steps to install the mailparser module via npm on a Windows 7 x64 system

Sorry if I am missing something obvious; this is a new error for me with npm: C:\work\spark3>npm install mailparser npm http GET https://registry.npmjs.org/mailparser npm http 304 https://registry.npmjs.org/mailparser npm http GET https://regis ...

Guide on sending an http request using Atlas Trigger in a NodeJS environment

I am working with a simple MongoDB Atlas trigger in NodeJS: exports = function(changeEvent) { // Implementing an HTTP request }; The main objective is to invoke an AWS lambda function from this trigger. Therefore, I am seeking guidance on the synta ...

Need a jQuery callback function in PHP that only retrieves the value without any redirection

Initially, I expected this task to be simple but it turns out my skills are a bit rusty. The snippet of javascript/jquery code that I am using is: $.get("plugin.php", {up_vote:surl}, function(data){ //alert(data); document.getElementById(&apo ...

Uncovering unseen tags generated by JavaScript on a webpage using Python

I have THIS LINK page that contains javascript. To view the javascript, simply click on show details. How can I extract data from this URL source? Should I use re? Here is what I attempted with re: import urllib import re gdoc = urllib.urlopen('Tha ...

The functionality of the Vue.js single file component is not performing as anticipated

Recently, I've been experimenting with Vue and Vue CLI. I came across this amazing vue-tabs-component that I would like to try out. Here is a snippet of my code from App.vue: <template> <div> <tabs> <tab name="Fir ...

Having difficulty managing asynchronous functions with the useState hook in React

import React from "react"; import { UserContext } from "./../contexts"; import { removeStoredAuthData, storedAuthIsValid, storeNewAuthData, } from "./../utils/auth"; import { getUserInfos } from "./../api/userAuthen ...

Ways to update row background color based on specific column values

I need to customize the background color of my table rows based on the value in the "Category" column. For example: Name Category Subcategory A Paid B C Received D If the Category value is 'Paid', I want the ro ...

Error: Google Plus Cross-Origin Resource Sharing Issue

I recently developed an AngularJS application that utilizes Google's server-side flow for authentication. The issue arises when my AngularJS app is hosted on one server and the Rest API is hosted on another. After successfully logging in, I encounter ...

Tips for calculating the canvas-relative mouse position on a CSS 3D transformed canvas

Recently decided to challenge myself by experimenting with drawing on 3D transformed canvases. After some trial and error, I managed to get it partially working. const m4 = twgl.m4; [...document.querySelectorAll('canvas')].forEach((canvas) =& ...

The command "npm install" does not automatically generate the node_modules directory, nor does it copy any .node files if the directory already exists

package.json { "name" : "xyzjs" "description" : "Sample project" "keywords": ["programming", "coding"], "version" : "1.0.0", "main": "./xyz.js" } this utilizes the binding.gyp in the current directory for project compilation, resulting in the ...

Transforming screen recording video chunks from blob into multipart for transmission via Api as a multipart

Seeking guidance in Angular 8 - looking for advice on converting screen recorded video chunks or blogs into a multipart format to send files via API (API only accepts multipart). Thank you in advance! ...

Initialize React Native project

Which ruby command shows the path to Ruby: /Users/User/.rbenv/shims/ruby Ruby -v command displays the version of Ruby installed: ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-darwin21] Which bundle command reveals the path to Bundler: /Users/Us ...

Is there a method available to incorporate a scroller into an nvd3 chart?

I am encountering an issue with my nvd3 chart. When I have a large amount of data that exceeds the width of the chart container, there is no scroll bar present and I'm struggling to figure out how to add one. I attempted to include overflow:scroll wi ...

Enabling NodeJS and Express to manage Cache-Control headers

I have a nodeJS server set up to provide resources through a REST API. Recently, one of the consumers of the API started including this in the header: "Cache-Control": "no-cache" This caused Node to reject the preflight options request with an error mess ...

Adding Bootstrap component via ajax request

I'm facing an issue with injecting a Bootstrap component using ajax. I usually include a select element like this: <select class="selectpicker" data-width="75%"> Most of the HTML code is generated dynamically through javascript, which you can ...

Difficulty encountered while attempting to deploy the front-end on Heroku

I recently completed a typeorm project with the following structure: https://i.stack.imgur.com/ReQK1.png Both the client and root directories contain index files located in the src directory. In the package.json file within the client directory, I made a ...

Optimal method for linking jQuery ajax requests to transfer data

Handling several asynchronous ajax calls in a specific order with information passing between them can be quite challenging. The current approach, even with just three API calls, can be cumbersome. Trying to manage five API calls makes it nearly impossible ...

Incorporating text sections into a div container and adjusting the width

Currently facing an issue with the canvas element on my project. <div id="app-container"> <div id="canvas-container"> <div id="canvas"></div> </div> </div> In the CSS stylesheet, the following styles ar ...

Confirm the email address using the autocomplete feature in the field

I'm currently utilizing Material UI to design an autocomplete field with multiple inputs that gives users the option to either choose from existing email addresses or input their own. An example of what I'm trying to achieve can be seen here: ht ...