Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions hjson-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
var quoteAlways = flag.Bool("quoteAlways", false, "Always quote string values.")
var showVersion = flag.Bool("v", false, "Show version.")
var preserveKeyOrder = flag.Bool("preserveKeyOrder", false, "Preserve key order in objects/maps.")
var preserveComments = flag.Bool("preserveComments", false, "Preserve comments in Hjson output (and key order in any output).")

flag.Parse()
if *help || flag.NArg() > 1 {
Expand Down Expand Up @@ -78,9 +79,11 @@ func main() {

var value interface{}

if *preserveKeyOrder {
if *preserveKeyOrder || *preserveComments {
var node *hjson.Node
err = hjson.Unmarshal(data, &node)
opt := hjson.DefaultDecoderOptions()
opt.WhitespaceAsComments = false
err = hjson.UnmarshalWithOptions(data, &node, opt)
value = node
} else {
err = hjson.Unmarshal(data, &value)
Expand Down Expand Up @@ -108,7 +111,7 @@ func main() {
opt.BracesSameLine = *bracesSameLine
opt.EmitRootBraces = !*omitRootBraces
opt.QuoteAlways = *quoteAlways
opt.Comments = false
opt.Comments = *preserveComments
out, err = hjson.MarshalWithOptions(value, opt)
if err != nil {
panic(err)
Expand Down
Loading