this post was submitted on 28 Jul 2025
14 points (100.0% liked)

Programming

21924 readers
760 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
 

I'm currently writing a CLI tool that handles a specific JSON data format. And I also want to give the user to get a slice of the item array of the file. It's a slice in form of --slice START:END through commandline options. So in example --slice 1:2.

  1. Should I provide a 0 based index for the access or a 1 based index? In example --slice 1:2 with 0 based index would start with the second element and with 1 based index it would start with the first element.
  2. And would you think its better to have the END to be inclusive or exclusive? In example --slice 1:2 would get only one element if its exclusive or it gets two elements if its inclusive.

I know this is all personal taste, but I'm currently just torn between all options and cannot decide. And thought to ask you what you think. Maybe that helps me sorting my own thoughts a bit. Thanks in advance.

you are viewing a single comment's thread
view the rest of the comments
[–] thingsiplay@beehaw.org 4 points 5 days ago (6 children)

First, thanks for the answer. As for the user base, its actually gaming oriented and they typically do not interact with 0 base. So I guess that makes for an obvious choice. And at the moment its also "inclusive". To get one element user needs to 2:2. If user gives only one element, such as 2, then I could convert it into 2:2, to get one element. Sounds logical, right? Sorry for having so many follow up questions, my head is currently spinning.

Do you think this interferes somehow with the logic of a "missing" slice element, which would default to "the rest of the list". In example 2: would then get the second element and until rest. This is the default behavior in Rust.

If I have a 1 based index, how would you interpret the 0? Currently program panics at Argument interpretation phase.

[–] stinky@redlemmy.com 2 points 5 days ago (2 children)

Microsoft's print dialog offers custom ranges of pages in an intuitive way, see above

You could demonstrate the numbering system for the users, maybe once at startup. Make it the first thing they see

[–] thingsiplay@beehaw.org 1 points 5 days ago (1 children)

No, that's not an option here. This is a commandline program, something like grep in example. And this slice is just one of the many features the user could use to refine the output.

[–] stinky@redlemmy.com 3 points 5 days ago

I mean to say the numbering system 1, 4, 7-12 to indicate slices of data, not the UI 🤣

other possibilities: 1, 4, 7-rest ("7 through the rest of the slices") or 1, 4, 7... ("7 through the rest of the slices")

and provide it as sample input to the user when they first try to print something, that should give them an idea of how to use your numbering system

load more comments (3 replies)