remove blockid from the regular args, use an optional -b (#973)

This commit is contained in:
Mike Sawka 2024-10-07 13:56:46 -07:00 committed by GitHub
parent 9d4df934c9
commit 26fcbe0f0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 12 additions and 15 deletions

View File

@ -11,7 +11,6 @@ import (
var deleteBlockCmd = &cobra.Command{ var deleteBlockCmd = &cobra.Command{
Use: "deleteblock", Use: "deleteblock",
Short: "delete a block", Short: "delete a block",
Args: cobra.ExactArgs(1),
Run: deleteBlockRun, Run: deleteBlockRun,
PreRunE: preRunSetupRpcClient, PreRunE: preRunSetupRpcClient,
} }
@ -21,11 +20,7 @@ func init() {
} }
func deleteBlockRun(cmd *cobra.Command, args []string) { func deleteBlockRun(cmd *cobra.Command, args []string) {
oref := args[0] oref := blockArg
if oref == "" {
WriteStderr("[error] oref is required\n")
return
}
err := validateEasyORef(oref) err := validateEasyORef(oref)
if err != nil { if err != nil {
WriteStderr("[error]%v\n", err) WriteStderr("[error]%v\n", err)

View File

@ -12,9 +12,9 @@ import (
) )
var getMetaCmd = &cobra.Command{ var getMetaCmd = &cobra.Command{
Use: "getmeta {blockid|blocknum|this} [key]", Use: "getmeta [key]",
Short: "get metadata for an entity", Short: "get metadata for an entity",
Args: cobra.RangeArgs(1, 2), Args: cobra.RangeArgs(0, 1),
Run: getMetaRun, Run: getMetaRun,
PreRunE: preRunSetupRpcClient, PreRunE: preRunSetupRpcClient,
} }
@ -24,7 +24,7 @@ func init() {
} }
func getMetaRun(cmd *cobra.Command, args []string) { func getMetaRun(cmd *cobra.Command, args []string) {
oref := args[0] oref := blockArg
if oref == "" { if oref == "" {
WriteStderr("[error] oref is required") WriteStderr("[error] oref is required")
return return
@ -44,8 +44,8 @@ func getMetaRun(cmd *cobra.Command, args []string) {
WriteStderr("[error] getting metadata: %v\n", err) WriteStderr("[error] getting metadata: %v\n", err)
return return
} }
if len(args) > 1 { if len(args) > 0 {
val, ok := resp[args[1]] val, ok := resp[args[0]]
if !ok { if !ok {
return return
} }

View File

@ -14,7 +14,7 @@ import (
var readFileCmd = &cobra.Command{ var readFileCmd = &cobra.Command{
Use: "readfile", Use: "readfile",
Short: "read a blockfile", Short: "read a blockfile",
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(1),
Run: runReadFile, Run: runReadFile,
PreRunE: preRunSetupRpcClient, PreRunE: preRunSetupRpcClient,
} }

View File

@ -35,6 +35,7 @@ var WrappedStdin io.Reader = os.Stdin
var RpcClient *wshutil.WshRpc var RpcClient *wshutil.WshRpc
var RpcContext wshrpc.RpcContext var RpcContext wshrpc.RpcContext
var UsingTermWshMode bool var UsingTermWshMode bool
var blockArg string
func extraShutdownFn() { func extraShutdownFn() {
if usingHtmlMode { if usingHtmlMode {
@ -174,6 +175,7 @@ func Execute() {
wshutil.DoShutdown("", 0, false) wshutil.DoShutdown("", 0, false)
} }
}() }()
rootCmd.PersistentFlags().StringVarP(&blockArg, "block", "b", "this", "for commands which require a block id")
err := rootCmd.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
wshutil.DoShutdown("", 1, true) wshutil.DoShutdown("", 1, true)

View File

@ -16,7 +16,7 @@ import (
var setMetaCmd = &cobra.Command{ var setMetaCmd = &cobra.Command{
Use: "setmeta {blockid|blocknum|this} key=value ...", Use: "setmeta {blockid|blocknum|this} key=value ...",
Short: "set metadata for an entity", Short: "set metadata for an entity",
Args: cobra.MinimumNArgs(2), Args: cobra.MinimumNArgs(1),
Run: setMetaRun, Run: setMetaRun,
PreRunE: preRunSetupRpcClient, PreRunE: preRunSetupRpcClient,
} }
@ -64,8 +64,8 @@ func parseMetaSets(metaSets []string) (map[string]interface{}, error) {
} }
func setMetaRun(cmd *cobra.Command, args []string) { func setMetaRun(cmd *cobra.Command, args []string) {
oref := args[0] oref := blockArg
metaSetsStrs := args[1:] metaSetsStrs := args[:]
if oref == "" { if oref == "" {
WriteStderr("[error] oref is required\n") WriteStderr("[error] oref is required\n")
return return