Coder Social home page Coder Social logo

Comments (5)

denisdefreyne avatar denisdefreyne commented on July 26, 2024

At the moment, Cri commands either accept subcommands, or accept arguments (an arbitrary number of them).

Do you need to have a command that can take both? (I imagine that could get confusing.)

from cri.

RoUS avatar RoUS commented on July 26, 2024

My use-case is a command configure with known subcommands (user, system, etc.). It's the subcommands that need to take arbitrary arguments -- otherwise I end up with something ugly like

configure user --option=foo --value=bar
configure user --option=format --value-yaml

as opposed to something like

configure user foo=bar format=yaml

This is oversimplified and somewhat contrived, but I hope it gets my idea across.

from cri.

denisdefreyne avatar denisdefreyne commented on July 26, 2024

@RoUS Would something along these lines work?

# frozen_string_literal: true

require 'cri'

config = {
  'animal' => 'cat',
  'sound' => 'meow',
}

config_cmd = Cri::Command.define do
  name        'conf'
  usage       'conf [subcommand] …'
  summary     'manage configuration'
  description '…'
end

set_cmd = Cri::Command.define do
  name        'set'
  usage       'set key=value ...'
  summary     'set config values'
  description '…'

  run do |opts, args|
    args.each do |string|
      key, value = string.split('=')
      config[key] = value
    end
  end
end

unset_cmd = Cri::Command.define do
  name        'unset'
  usage       'unset key ...'
  summary     'unset config values'
  description '…'

  run do |opts, args|
    args.each do |key|
      config.delete(key)
    end
  end
end

config_cmd.add_command(set_cmd)
config_cmd.add_command(unset_cmd)

config_cmd.run(ARGV)

pp config
% ruby conf.rb unset animal
{"sound"=>"meow"}
% ruby conf.rb unset asdf
{"animal"=>"cat", "sound"=>"meow"}
% ruby conf.rb set fn=Denis ln=Defreyne
{"animal"=>"cat", "sound"=>"meow", "fn"=>"Denis", "ln"=>"Defreyne"}

from cri.

RoUS avatar RoUS commented on July 26, 2024

Yes, I think that would do it; I totally missed the concept of using #run in the command definition. Thanks!

from cri.

denisdefreyne avatar denisdefreyne commented on July 26, 2024

Cool! I’ll close this issue now.

from cri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.